QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#627#413473#8671. 分流器MilmonANIGFailed.2024-05-22 20:15:212024-05-22 20:15:28

Details

Extra Test:

Accepted
time: 0ms
memory: 3652kb

input:

5
2 3
4 5
4 5
5 6
6 6
0 0 0 0 0

output:

8

result:

ok single line: '8'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#413473#8671. 分流器ANIG97 11ms4716kbC++231.6kb2024-05-17 16:43:302024-05-26 03:01:33

answer

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned short
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);
}
signed main(){
    cin.tie(nullptr),cout.tie(nullptr)->sync_with_stdio(false);
	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={1};
  int cnt=dp[n+1]-1;
  while(cnt--){
    for(int i=0; i<ans.size(); i++){
      ans[i]=ans[i]*2;
    }
    for(int i=0; i<ans.size(); i++){
      if(i==ans.size()-(int)(1)){
        if(ans[i]/10){
          ans.emplace_back(0);
        }
      }
      ans[i+1]+=ans[i]/10;
      ans[i]%=10;
    }
  }
  reverse(ans.begin(),ans.end());
  for(auto p:ans) cout<<p;
  cout<<'\n';
}