hdu 3357 Stock Chase (图论froyd变形)

Stock Chase

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


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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3356 3358 3359 3360 3351 
 

 

 1 //250MS    496K    793 B    G++
 2 /*
 3 
 4     题意:
 5         给出n个公司的联系,给出m个关系(单向图),要求不能形成环,
 6     问要去掉多少个关系。
 7  
 8     froyd变形:
 9         此题解题形式类似froyd,属于图论题。
10         思路不难,先记录其每一次的关系,有矛盾则去掉,没矛盾加入,
11     并且更新图。
12         更新情况:
13          1、  g[i][a]&&g[a][b]&&g[b][j]=>g[i][j]
14          2、  g[i][a]&&g[a][b]=>g[i][b]
15          3、  g[a][b]&&g[b][i]=>g[a][i]
16          
17      时间复杂度应为O(n*n*m) = =! 
18 
19 */
20 #include<stdio.h>
21 #include<string.h>
22 int g[250][250];
23 int n,m;
24 int main(void)
25 {
26     int a,b,k=1;
27     while(scanf("%d%d",&n,&m),m+n)
28     {
29         memset(g,0,sizeof(g));
30         int cnt=0;
31         while(m--){
32             scanf("%d%d",&a,&b);
33             if(g[b][a] || a==b){
34                 cnt++;continue;
35             }
36             if(g[a][b]) continue;
37             g[a][b]=1;
38             for(int i=1;i<=n;i++){
39                 if(g[i][a])
40                 for(int j=1;j<=n;j++){
41                     if(g[b][j]) g[i][j]=1;
42                 }        
43             }
44             for(int i=1;i<=n;i++){
45                 if(g[i][a]) g[i][b]=1;
46                 if(g[b][i]) g[a][i]=1;
47             }
48         }
49         printf("%d. %d\n",k++,cnt);   
50     }
51     return 0;
52 }

 

posted @ 2014-03-26 17:32  heaventouch  阅读(322)  评论(0编辑  收藏  举报