Posted on 2010-10-04 23:08
成幸毅 阅读(222)
评论(0) 编辑 收藏 引用
Stock Chase |
Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB |
Total submit users: 28, Accepted users: 20 |
Problem 11785 : No special judgement |
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.
|
Sample Input |
3 6
1 2
1 3
3 1
2 1
1 2
2 3
0 0
|
Sample Output |
1. 2
|
解题报告:开始想得太复杂了,这道题有点像上次天津网络赛那个最短路,类似于floyd那样做的,那是点,这是边,每次添加一条边,让后去判断是否有环,有环就不添加,并且计数加1
#include <iostream>
using namespace std;
const int MAXN = 500;
int n,m;
int r[MAXN][MAXN];
int a,b;
int main() {
int cas=1;
while(scanf("%d%d",&n,&m) != EOF) {
if(n == 0 && m == 0) break;
memset(r,0,sizeof(r));
int ans = 0;
for(int i = 1; i <= n; i++) r[i][i] = 1;
for(int i = 1; i <= m; i++) {
scanf("%d%d",&a,&b);
if(r[b][a]) {
ans++;
continue;
}
if(r[a][b]) continue;
//r[a][b] = 1;
for(int j = 1; j <= n; j++) {
for(int k = 1; k <= n; k++) {
r[j][k] = r[j][k] || (r[j][a] && r[b][k]);
}
}
}
printf("%d. %d\n",cas++,ans);
}
// system("pause");
return 0;
}