标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 58979 | Accepted: 17247 |
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
经典题:练了下刚刚的向量偏移 ,以前不会做的,poj炸了,还没测试
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; const int N =50010; int father[N]; int relation[N]; ///relation[] 为0表示同类 1表示被根节点吃 2表示吃根节点 int _find(int x){ if(x!=father[x]){ int t = father[x]; father[x] = _find(father[x]); relation[x]=(relation[x]+relation[t])%3; ///对3取余防止出现大于2的情况 } return father[x]; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<=n;i++){ father[i] = i; relation[i] = 0; ///自己与自己当然是同类 } int ans = 0; while(m--){ int d,a,b; scanf("%d%d%d",&d,&a,&b); if(a>n||b>n||(a==b)&&(d==2)) { ans++; continue; } int roota = _find(a); int rootb = _find(b); if(roota==rootb){ ///当前结点已经是同一棵数了 if(d==1&&relation[a]!=relation[b]) ans++; ///相同种类但relation不同 if(d==2&&relation[a]!=(relation[b]+2)%3) ans++; }else{ father[rootb] = roota; relation[rootb] = (relation[a]+(d-1)-relation[b]+3)%3; } } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5327376.html