POJ3997Stock Chase

Stock Chase
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 453 Accepted: 129

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

Source

anarc 2009

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 260;
int n,t,ans;
bool mat[maxn][maxn];
void init(){
    ans=0;
    memset(mat,0,sizeof(mat));
    for(int i = 1; i <= n; i++){
        mat[i][i] = 1;
    }
}
int main(){

    int T=1;
    while(~scanf("%d%d",&n,&t) &&n+t){
        init();
        while(t--){
            int a,b;
            scanf("%d%d",&a,&b);
            if(mat[b][a]){
                ans++;
            }
            else if(!mat[a][b]){
                mat[a][b] = 1;
                for(int i = 1; i <= n; i++){
                    if(mat[i][a]){
                        mat[i][b] = 1;
                        for(int j = 1; j <= n; j++)
                            if(mat[b][j]) mat[i][j] = 1;
                    }
                }
            }

        }
        printf("%d. %d\n",T++,ans);

    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值