1. 程式人生 > >Codeforces Round #332 (Div. 2) E. Sandy and Nuts

Codeforces Round #332 (Div. 2) E. Sandy and Nuts

http://codeforces.com/contest/599/problem/E

題意不再多說...

我用 dp[i][type][s] 表示以i為根,包含二進位制s所表示的節點(不含i)的子樹的方案數,type的作用放到後面解釋。

那麼答案就是 dp[0][0][(1<<n)-2]

狀態轉移的時候我先分離出當前sta最低位的1(lowbit), 然後我列舉和這一位組成一顆子樹的情況(這樣使得列舉不會重複,隊友說的。。)。

對於情況s,要判斷是否有效,我列舉以i為公共祖先的點對,要求不能同時在s裡,並且對於每個s中的點,與它相連的點要在s中或者等於i。

對於符合條件的s, 列舉某個點j作為子樹的根, dp[i][type][sta] += dp[j][0][s] * dp[i][1][sta -s].

type == 0 表示未被分割的情況,type== 1表示另外還有子樹連著根。這是為了當type==0時,我要首先判斷一下以i為公共祖先的點對要都在點集裡。

遞迴結束條件是sta == 0 時 return 1。

(感覺並沒有講清楚T^T)

#include 

using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=int(s); i pii;

int n,m,q;
int linked[15];
ll dp[15][3][1<<13+5];
pii mlca[15][105];
int cnt_lca[15];

bool judge(int root, int s, int mods)
{
	rep(i, 0, n) if((s >> i) & 1)
	{
		if((linked[i] | mods) != mods) return false;
	}
	rep(i, 0, cnt_lca[root])
	{
		if(((s >> mlca[root][i].first) & 1) && ((s >> mlca[root][i].second) & 1)) return false;
	}
	return true;
}
ll dpit(int root, int beg, int sta) // root 0 ~ n-1
{
	if(~dp[root][beg][sta]) return dp[root][beg][sta];
	if(beg == 0)
	{
		int tmps = sta + (1 << root);
		rep(i, 0, cnt_lca[root])
		{
			if(((tmps >> mlca[root][i].first) & 1) && ((tmps >> mlca[root][i].second) & 1)) continue;
			return dp[root][beg][sta] = 0;
		}
	}
	if(sta == 0)
	{
		return 1;
	}
	ll& ret = dp[root][beg][sta];
	ret = 0;
	int has = (sta & (-sta));
	sta -= has;
	for(int s = 0; s<=sta; s++) if((s | sta) == sta)
	{
		int ss = s + has;
		if(!judge(root, ss, ss + (1 << root))) continue;
		for(int i=0; i> i) & 1)
		{
			ret += dpit(i, 0, ss - (1 << i)) * dpit(root, 1, sta - s);
		}
	}
	//cout<

相關推薦

Codeforces Round #332 (Div. 2) E. Sandy and Nuts

http://codeforces.com/contest/599/problem/E 題意不再多說... 我用 dp[i][type][s] 表示以i為根,包含二進位制s所表示的節點(不含i)的子樹的方案數,type的作用放到後面解釋。 那麼答案就是 dp[0][0][(

[Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)

solution and other main ems scanf homework max urn Description Pashmak‘s homework is a problem about graphs. Although he always tries

(博弈sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

paths 相同 friend pri 產生 chan star ren have Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered

Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩陣)

clu blank his 題目 pri 狀態 oid c++ class 題目鏈接:Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 題意: 在一個二維方格子裏有n條線段,有三種走法 (x?+?1,?y?

Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree(樹上Nim)

ide vector 等於 如果 ace ble 技術 方式 復雜度 題目鏈接:Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree 題意: 給你一棵樹,每個節點有a[i]個蘋果,有兩個人要在這個樹上玩遊戲。 兩個

Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(預處理+二分)

class [1] can 行處理 long 個數 分享 amp abs 題目鏈接:點我點我 題意:公式:,給出n個數,從a[1]到a[n],和m個數(b數組),然後從b數組裏挑出連續的n個數(也就m-n+1中選擇),按公式計算,使得f最小, 還有q次對a數組的操作(某

Codeforces Round #384 (Div. 2) E. Vladik and cards 狀壓dp

答案 clu test none urn 數字 ble inline return Codeforces Round #384 (Div. 2) E. Vladik and cards 狀壓dp 大致題意: 給定一個序列an,序列中只有1~8的8個整數,讓你選出一個子序列

Codeforces Round #321 (Div. 2) E - Kefa and Watch

back fin put 我們 esp bool cond can push_back 題目大意:給你一個由0-9組成的字符串,有m個詢問,兩種操作,第一種將l到r的字符全部變成c,第二種問l到r這段 字符串的循環節是不是d。 思路:首先我們要知道怎麽判斷字符串的循環節的長

Codeforces Round #379 (Div. 2) E. Anton and Tree

vector #define clu cto bits air int 向上 fin 題意:給你一棵樹, 每個點要麽是黑色要麽是白色, 有一種操作是將同一個顏色的連通塊變成相反的顏色,問你最少變換幾次, 整顆樹變成一種顏色。 思路: 縮點, 加求樹的直徑, 答案為樹

Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans

tdi mat str ng- first ces AI codeforce efi 這道題的算法是: i從1開始,首先求sum(1-i),然後在[i+1, n]中找到第一個a[j]>=sum(1, i) 如果a[j]==sum(1, i)結束搜索,否則令i=j,循環

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫隊算法 + 異或和前綴和的巧妙】

lag ans integer ons lap stand 推出 open closed 任意門:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit p

Educational Codeforces Round #54 (Div. 2) E. Vasya and a Tree 技巧題

題目連線:http://codeforces.com/contest/1076/problem/E   本題大意:   給一棵根節點為“1”樹,給m個操作,每個操作三個整數,v,d,x,意思是從節點1,往下深度d,遍及的節點的值都加上x,d可能是0,就是隻加在自己上。結束m個操作後輸出每個節點的值。 &

Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(lca+思維,樹上尋找與給定兩個點距離相等的點的個數)

E. A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

Codeforces Round #303 (Div. 2) E. Paths and Trees

Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this pr

Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

E. Paths and Trees Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important th

Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem(貪心+樹形dp)

E. Ehab and a component choosing problem time limit per test 1 second memory limit per test 256 megabytes input standard input output

Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(樹形dp)

E. Ehab and a component choosing problem time limit per test 1 second memory limit per test 256 megabytes input standard input output

Codeforces Round #532 (Div. 2)E. Andrew and Taxi

題意:給一個有向圖,反轉某些邊讓這個圖無環,反轉的花費是反轉的所有邊中權值最大的,求花費最小的方式,輸出花費和需要反轉的邊。 思路:把某個邊反轉其實就相當於把這條邊刪除,二分答案,用拓撲排序判斷是否又環,最後刪完了之後是ACG,求每個點的拓撲序,然後檢驗刪去的邊是否能形成環。 #includ

Codeforces Round #261(Div 2) E Pashmak and Graph(圖中嚴格遞增的最長路徑、思維)

題目連結 題意 給一個n個點和m條帶權值的有向邊的圖。保證無自環和重邊。在圖中找到最長的一條有向路徑,使得路徑中的邊權是嚴格遞增的,求路徑的最大長度(路徑中邊的數量)。 資料範圍:2≤n≤

Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks

取出 splay names mem pri 思路 turn const gpo 題目大意:有m (m<=1e9) 個相同的塊,每個塊裏邊有n個數,每個數的範圍是1-9,從每個塊裏邊取出來一個數組成一個數,讓你求組成的方案中 被x取模後,值為k的方案數。(1<=