QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#632#413160#8671. 分流器MilmonANIGSuccess!2024-05-22 22:29:272024-05-22 22:29:28

详细

Extra Test:

Time Limit Exceeded

input:

50000
2 50001
3 50001
4 50001
5 50001
6 50001
7 50001
8 50001
9 50001
10 50001
11 50001
12 50001
13 50001
14 50001
15 50001
16 50001
17 50001
18 50001
19 50001
20 50001
21 50001
22 50001
23 50001
24 50001
25 50001
26 50001
27 50001
28 50001
29 50001
30 50001
31 50001
32 50001
33 50001
34 50001
35 50...

output:


result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#413160#8671. 分流器ANIG#97 27ms5928kbC++141.6kb2024-05-17 08:53:232024-05-26 03:00:47

answer

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define int long long
const int N=5e4+5;
int n,to[N][2],dp[N],rd[N];
ull f[N];
ull pows(int a,int b){
	if(b==0)return 1;
	ull res=pows(a,b>>1);
	res=res*res;
	if(b&1)res=res*a;
	return res;
}
deque<int>q;
int lcm(int a,int b){
	if(!a||!b)return a|b;
	return a*b/__gcd(a,b);
}
vector<int>add(vector<int>a,vector<int>b){
	vector<int>c;
	int n=max(a.size(),b.size())+2;
	c.resize(n);a.resize(n);b.resize(n);
	for(int i=0;i<n;i++){
		c[i]+=a[i]+b[i];
		if(i<n-1)c[i+1]+=c[i]/10;
		c[i]%=10;
	}
	while(c.back()==0)c.pop_back();
	return c;
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>to[i][0]>>to[i][1],rd[to[i][0]]++,rd[to[i][1]]++;
	for(int i=1;i<=n;i++){
		int op;
		cin>>op;
		if(op)swap(to[i][0],to[i][1]);
	}
	q.push_back(1);
	dp[1]=1;f[1]=1;
	while(q.size()){
		int x=q.front();
		q.pop_front();
		if(f[x]&1)dp[x]++,f[x]<<=1;
		int c=to[x][0],k=max(dp[x],dp[c]);
//		cout<<x<<" "<<dp[x]<<" "<<f[x]<<endl;
		if(dp[c]){
			f[c]=f[c]*pows(2,k-dp[c])+(f[x]>>1)*pows(2,k-dp[x]);
		}else{
			f[c]=(f[x]>>1)*pows(2,k-dp[x]);
		}
		dp[c]=k;
		rd[c]--;
		if(!rd[c])q.push_back(c);
		c=to[x][1],k=max(dp[x],dp[c]);
		if(dp[c]){
			f[c]=f[c]*pows(2,k-dp[c])+(f[x]>>1)*pows(2,k-dp[x]);
		}else{
			f[c]=(f[x]>>1)*pows(2,k-dp[x]);
		}
		dp[c]=k;
		rd[c]--;
		if(!rd[c])q.push_back(c);
	}
	vector<int>ans;
	ans.push_back(1);
	for(int i=1;i<dp[n+1];i++)ans=add(ans,ans);
	while(ans.back()==0)ans.pop_back();
	while(ans.size())cout<<ans.back(),ans.pop_back();
}