Poj 1182 with permission and query set
Food Chain
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:45303 |
|
Accepted:13213 |
Description The animal kingdom contains three types of animals A, B, and C. The food chains of these three types constitute an interesting ring. A eats B, B eats C, and C eats. There are N animals numbered 1-N. Every animal is one of A, B, and C, but we don't know which one it is. There are two ways to describe the relationship between the food chains of the N animals: The first statement is "1 x y", indicating that X and Y are similar. The second statement is "2 x y", which indicates that X eats Y. This person speaks K sentences one by one for N animals in the preceding two statements. These K sentences are true or false. When one sentence meets the following three conditions, this sentence is a lie, otherwise it is the truth. 1) The current statement conflicts with some of the preceding actual statements; 2) In the current statement, X or Y is greater than N, which is false; 3) The current statement indicates that X eats X, which is a lie. Your task outputs the total number of false statements based on the given N (1 <= N <= 50,000) and K statements (0 <= K <= 100,000.
Input The first line is two integers N and K, separated by a space. Each row in the following K rows contains three positive integers, D, X, and Y, which are separated by a space. D indicates the type of the statement. If D = 1, X and Y are of the same type. If D = 2, X eats Y.Output Only one integer indicates the number of false statements.Sample Input 100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5 Sample Output 3 Source Noi 01 |
[Submit] [Go Back] [Status] [Discuss]
Storage
#include
#include#include
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn= 50005;typedef long long LL;int p[maxn],d[maxn],n,k;void init(int n){ for(int i=1;i<=n;i++){ p[i]=i; d[i]=0; }}int findx(int x){ if(p[x]==x)return x; int root=findx(p[x]); d[x]+=d[p[x]]; d[x]%=3; return p[x]=root;}bool Union(int x,int y,int op){ if(x>n||y>n)return false; int u=findx(x),v=findx(y); if(u!=v) { p[u]=v; d[u]=(d[y]+op-d[x]+3)%3; return true; } return d[x]==(d[y]+op)%3;}int main(){ scanf("%d%d",&n,&k); init(n); int x,y,d,ans=0; while(k--) { scanf("%d%d%d",&d,&x,&y); if(!Union(x,y,d-1))ans++; } printf("%d\n",ans); return 0;}