posts - 129,comments - 0,views - 37664

Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 152 Accepted Submission(s): 65

Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
 

Input
There are many test cases:
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

 

Output
For every case:
Output R, represents the number of incorrect request.
 

Sample Input
10 10
1 2 150
3 4 200
1 5 270
2 6 200
6 5 80
4 7 150
8 9 100
4 8 50
1 7 100
9 2 100
 

Sample Output
2
Hint
Hint: (PS: the 5th and 10th requests are incorrect)

思路:这道题跟 5.1.8 How Many Answers Are Wrong 一模一样,我只是把y++这句删了就过了。、

复制代码
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 using namespace std;
 8 
 9 const int maxn=200100;
10 int f[maxn],sum[maxn],a,b,x,y,fx,fy,n,m,v,ans;
11 
12 void close()
13 {
14     exit(0);
15 }
16 
17 int find(int k)
18 {
19     if (f[k]==k)
20         return k;
21     int t=f[k];
22     f[k]=find(f[k]);
23     sum[k]+=sum[t];
24     return f[k];
25 }
26 
27 void work()
28 {
29 }    
30 
31 void init ()
32 {
33     while(scanf("%d %d",&n,&m)!=EOF)
34      {
35          ans=0;
36          if (n==0 && m==0) break;
37          for (int i=0;i<=n+2;i++)
38          {
39              f[i]=i;
40              sum[i]=0;
41          }
42          for (int i=1;i<=m;i++)
43          {
44              scanf("%d %d %d",&x,&y,&v);
45              fx=find(x);
46              fy=find(y);
47              if (fx==fy && v+sum[y]!=sum[x])
48                  ans++;
49              else
50                  if(fx!=fy)
51                  {
52                      if (fy>fx)
53                      {
54                         f[fx]=fy;
55                         sum[fx]=sum[y]+v-sum[x];
56                      }
57                      else
58                      {
59                          f[fy]=fx;
60                          sum[fy]=sum[x]-v-sum[y];
61                      }
62                  }
63          }
64          printf("%d\n",ans);
65      }
66 }
67 
68 int main ()
69 {
70     init();
71     work();
72     close();
73     return 0;
74 }
复制代码

 

posted on   cssystem  阅读(150)  评论(0)    收藏  举报
< 2013年2月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示