标签:
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Description
Input
Output
Sample Input
Sample Output
Hint
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
思路分析:需要维护的信息除了父亲节点,还要维护它与其父亲节点的关系,中间有几个关系不太好推
可以参照一下这个博客http://blog.csdn.net/c0de4fun/article/details/7318642/
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=50000+100; int father[maxn],relat[maxn];//关系数组,表明它与父节点的关系,0代表同类,1代表被父节点吃,2代表吃父节点 int n,k; int findroot(int x) { if(x==father[x]) return x; int oldfa=father[x]; father[x]=findroot(father[x]); relat[x]=(relat[x]+relat[oldfa])%3;//更新压缩路径后它与父节点的关系 return father[x]; } void merge(int d,int x,int y) { int fx=findroot(x); int fy=findroot(y); if(fx==fy) return; father[fx]=fy; relat[fx]=(relat[y]+d-relat[x]+3)%3; return; } bool istrue(int d,int x,int y) { if(x>n||y>n||((d==2)&&(x==y))) return false; int fx=findroot(x),fy=findroot(y); if(fx!=fy)//两者关系待定 return true; else { if(relat[x]==(relat[y]+(d-1))%3) return true; else return false; } } int main() { int d,a,b; scanf("%d%d",&n,&k); int ans=0; memset(relat,0,sizeof(relat)); memset(father,0,sizeof(father)); for(int i=1;i<=n;i++) { father[i]=i; relat[i]=0; } for(int i=1;i<=k;i++) { scanf("%d%d%d",&d,&a,&b); if( !istrue(d,a,b) ) ans++; else merge(d-1,a,b); } printf("%d\n",ans); }
标签:
原文地址:http://www.cnblogs.com/xuejianye/p/5700354.html