5.2.5 Stock Chase

Stock Chase

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

Problem Description
I have to admit, the solution I proposed last year for solving the bank cash crisis didn’t solve the whole economic crisis. As it turns out, companies don’t have that much cash in the first place.
They have assets which are primarily shares in other companies. It is common, and acceptable, for one company to own shares in another. What complicates the issue is for two companies to own shares in each other at the same time. If you think of it for a moment, this means that each company now (indirectly) controls its own shares.
New market regulation is being implemented: No company can control shares in itself, whether directly or indirectly. The Stock Market Authority is looking for a computerized solution that will help it detect any buying activity that will result in a company controlling its own shares. It is obvious why they need a program to do so, just imagine the situation where company A buying shares in B, B buying in C, and then C buying in A. While the first two purchases are acceptable.
The third purchase should be rejected since it will lead to the three companies controlling shares in themselves. The program will be given all purchasing transactions in chronological order. The program should reject any transaction that could lead to one company controlling its own shares.
All other transactions are accepted.
 

Input
Your program will be tested on one or more test cases. Each test case is specified on T + 1 lines. The first line specifies two positive numbers: (0 < N <= 234) is the number of companies and (0 < T <= 100, 000) is the number of transactions. T lines follow, each describing a buying transaction. Each transaction is specified using two numbers A and B where (0 < A,B <= N) indicating that company A wants to buy shares in company B.
The last line of the input file has two zeros.
 

Output
For each test case, print the following line:
k. R
Where k is the test case number (starting at one,) R is the number of transactions that should be rejected.
Note: There is a blank space before R.
 

Sample Input
3 6
1 2
1 3
3 1
2 1
1 2
2 3
0 0
 

Sample Output
1. 2

思路:中文意思是,给你T个公司之间的关系,如果a公司买的b公司的股票不是a公司的,那么就可以,否则不行,求出不行的个数。

这道题我就呵呵呵了,做了一晚上,简直就是水的不得了,其实我想到了 x==y的情况,但我认为数据不会这样啊!结果就挂了TAT,就说你每一次操作时,要记得将所有的点都更新完,否则就会WA哦~

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 using namespace std;
 8 
 9 int ans,cas,n,m,fx,fy,x,y,cnt;
10 bool f[240][240];
11 int a[240];
12 
13 
14 void close()
15 {
16     exit(0);
17 }
18 
19 void work()
20 {
21 }
22 
23 void init ()
24 {
25     while(scanf("%d %d",&n,&m)!=EOF)
26      {
27          if (n==0 && m==0) break;
28          memset(f,false,sizeof(f));
29          ans=0;cas++;
30          for (int i=1;i<=m;i++)
31          {
32              scanf("%d %d",&x,&y);
33              if (f[y][x] || y==x)//就是这里,坑了我好久!
34                  ans++;
35              else
36              {
37                  cnt=0;
38                  for (int j=1;j<=n;j++)
39                      if (f[y][j])
40                      {
41                          f[x][j]=true;
42                          cnt++;
43                          a[cnt]=j;
44                      }
45                  f[x][y]=true;
46                  cnt++;
47                  a[cnt]=y;
48                  for (int j=1;j<=n;j++)
49                      if (f[j][x])
50                      {
51                          for (int k=1;k<=cnt;k++)
52                              f[j][a[k]]=true;
53                      }
54              }
55          }
56          printf("%d. %d\n",cas,ans);
57      }
58 }
59 
60 int main ()
61 {
62     init();
63     work();
64     close();
65     return 0;
66 }
原文地址:https://www.cnblogs.com/cssystem/p/2919748.html