CodingTrip

剪刀石头布

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

现有M个人一起玩剪刀石头布,以1-M编号,每人出一种,出过不再改变,但是我们并不知道它到底是哪一种。 (其中石头赢剪刀,剪刀赢布,布赢石头,一样则平)
裁判用两种说法对这M个人所构成的输赢关系进行描述: 
一:"1 A B",表示第A个人和第B个人出的一样。 
二:"2 A B",表示第A个人赢第B个人。 
裁判对M个人,用以上两种说法,连说N句话,其中有真的、也有假的。
一句话出现以下情况,就是假话,否则就是真话。 
1) 该句话与之前的某些真话冲突; 
2) 该句话中A或B比M大; 
3) 该句话表示A赢A。 

请根据给定的M和N,输出假话数。
其中(1 <= M <= 10,000),(0 <= N <= 10,000)

Input

第1行是一个自然数K,代表有K组数据。
每组数据以一个空行分隔,其中每组数据的第1行是两个自然数M、N,以空格分开。 
每组数据的第2行至N+1行,每行是三个自然数X,A,B,三个数之间用空格分开,X(1或2)表示说法的种类。

Output

每组数据对应一行,每行有一个整数,代表假话数。

Sample Input

3

43 11
1 4 3
2 3 3
1 4 1
1 4 4
2 3 3
1 2 2
2 1 4
1 1 1
2 1 4
2 3 4
2 3 2

66 9
2 3 1
2 4 4
2 1 2
2 4 3
2 4 2
2 2 3
1 3 2
1 2 1
1 1 1

6 7
2 3 7
2 1 2
2 4 4
1 2 1
1 3 2
1 2 3
2 1 3

Sample Output

5
4
3

详见POJ1182···(PS:携程好多陈题,传说第二题也是POJ上面的题···)
  1 /*
  2 ID: asif
  3 LANG: C++
  4 TASK: test
  5 */
  6 //# pragma comment(linker, "/STACK:102400000,102400000")
  7 # include<iostream>
  8 # include<cstdio>
  9 # include<cstdlib>
 10 # include<cstring>
 11 # include<algorithm>
 12 # include<cctype>
 13 # include<cmath>
 14 # include<string>
 15 # include<set>
 16 # include<map>
 17 # include<stack>
 18 # include<queue>
 19 # include<vector>
 20 # include<numeric>
 21 using namespace std;
 22 const int maxn=100010;
 23 const double inf=0.000001;
 24 const int INF=~0U>>1;
 25 const int mod=1000000007;
 26 # define PI (acos(0)*2.0)
 27 # define lson l,m,rt<<1
 28 # define rson m+1,r,rt<<1 | 1
 29 # define PS printf("
")
 30 # define S(n) scanf("%d",&n)
 31 # define P(n) printf("%d
",n)
 32 # define Ps(n) printf(" %d",(n))
 33 # define SB(n) scanf("%lld",&n)
 34 # define PB(n) printf("%lld
",n)
 35 # define PBs(n) printf(" %lld",n)
 36 # define SD(n) scanf("%lf",&n)
 37 # define PD(n) printf("%.3lf
",n)
 38 # define Sstr(s) scanf("%s",s)
 39 # define Pstr(s) printf("%s
",s)
 40 # define S0(a) memset(a,0,sizeof(a))
 41 # define S1(a) memset(a,-1,sizeof(a))
 42 typedef long long ll;
 43 int n,f[maxn],p[maxn],m;
 44 int equal(double x,double y)
 45 {
 46     if(x-y>=-inf&&x-y<=inf)
 47         return 0;
 48     else if(x-y>inf)
 49         return 1;
 50     return -1;
 51 }
 52 void init()
 53 {
 54     for(int i=0;i<=n;i++)
 55         f[i]=i,p[i]=0;
 56 }
 57 int find(int x)
 58 {
 59     if(x==f[x])
 60         return x;
 61     int fx=find(f[x]);
 62     p[x]=(p[x]+p[f[x]])%3;
 63     f[x]=fx;
 64     return f[x];
 65 }
 66 bool unin(int a,int x,int y)
 67 {
 68     a--;
 69     int fx=find(x);
 70     int fy=find(y);
 71     if(fx==fy)
 72     {
 73         if((p[y]-p[x]+3)%3!=a)
 74             return true;
 75         return false;
 76     }
 77     f[fy]=fx;
 78     p[fy]=(p[x]-p[y]+3+a)%3;
 79     return false;
 80 }
 81 int main()
 82 {
 83     //freopen("input.in", "r", stdin);
 84     //freopen("output.out", "w", stdout);
 85     int T;
 86     S(T);
 87     while(T--)
 88     {
 89         S(n),S(m);
 90         init();
 91         int ans=0;
 92         while(m--)
 93         {
 94             int a,u,v;
 95             S(a),S(u),S(v);
 96             if(u>n||v>n||(a==2&&u==v))
 97                 ans++;
 98             else
 99             {
100                 if(unin(a,u,v))
101                     ans++;
102             }
103         }
104         P(ans);
105     }
106     return 0;
107 }
View Code
原文地址:https://www.cnblogs.com/asif/p/3659995.html