NC223525. DanceCircle
描述
输入描述
The first line of the input consists of a single integer n, indicating that there are n children in the ring (1 ≤ n ≤200 000). The following n lines describe the children you can see at different times. The line (indexed startingfrom zero) contains three space-separated non-negative integers : you can see children when child is in the center of view (lito the left and to the right of child ). If xi = 0 then aneven number of them are wearing the orange pumpkin costume. If = 1 then an odd number of them are wearingthe orange pumpkin costume.
输出描述
Compute the number of ways of assigning a costume to each child, consistent with your observations. Since thisnumber might be large, print the result modulo . (If it’s impossible to find any costume assignment thatmatches all parity constraints, print 0).
示例1
输入:
5 1 0 0 1 0 1 3 0 1 3 0 0 3 0 1
输出:
0
示例2
输入:
5 3 1 1 0 3 1 1 3 1 1 2 1 0 4 1
输出:
4
C++ 解法, 执行用时: 518ms, 内存消耗: 79384K, 提交时间: 2021-08-19 13:59:27
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; vector<int>to[N+N][2],t2[N+N][2]; int n,vis[N*2],val[N*2]; void add(int x,int y,int z){ to[x][z].push_back(y); to[y][z].push_back(x); } bool dfs(int x){ for(int y:to[x][0]){ if(vis[y]&&val[y]!=val[x]){ return 0; } if(!vis[y]){ vis[y]=1; val[y]=val[x]; if(!dfs(y))return 0; } } for(int y:to[x][1]){ if(vis[y]&&val[y]==val[x]){ return 0; } if(!vis[y]){ vis[y]=1; val[y]=1^val[x]; if(!dfs(y))return 0; } }return 1; } int work(int vn){ for(int i=1;i<=n+n;i++)t2[i][0]=to[i][0],t2[i][1]=to[i][1],vis[i]=val[i]=0; for(int i=1;i<=n;i++) add(i,i+n,vn); vis[n+n]=1;val[n+n]=0; int ans=1; if(!dfs(n+n))ans=0; for(int i=1;i<=n+n;i++)if(!vis[i]){ vis[i]=1;ans=(ans<<1)%1000000007; if(!dfs(i))ans=0; } for(int i=1;i<=n+n;i++)to[i][0]=t2[i][0],to[i][1]=t2[i][1],vis[i]=val[i]=0; return ans; } int main(){ scanf("%d",&n); for(int x,y,z,i=1;i<=n;i++){ scanf("%d%d%d",&x,&y,&z); int l=i-x-1,r=i+y; if(l<1)l+=n,r+=n; add(l,r,z); } int ans=(work(0)+work(1))%1000000007; printf("%d\n",ans); return 0; }