Stock Chase--HDOJ

来源:互联网 发布:yy主播拍的网络大电影 编辑:程序博客网 时间:2024/05/17 09:19

Stock Chase
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 374 Accepted Submission(s): 131

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
2009 ANARC

Recommend
lcy

#include<iostream>#include<stdio.h>#include<algorithm>#include<string>#include<string.h>#include<math.h>#include<map>#include<set>#include<string>#define mod 1000000007using namespace std;int ans,n,t;int link[250][250];void Update(int a,int b){    link[a][b] = 1;    int numa=0,numb=0,aa[250],bb[250];    for(int i=1; i<=n; ++i)    {        if(link[i][a])//能到a的,也能到b        {            link[i][b] = 1;            aa[numa++] = i;        }        if(link[b][i])//b能到的,a也能到        {            link[a][i] = 1;            bb[numb++] = i;        }    }    //能到a的点,也能到b所能到的点    for(int i=0; i<numa; ++i)    {        for(int j=0; j<numb; ++j)        {            link[aa[i]][bb[j]] = 1;        }    }}int main(void){//    freopen("in.txt","r",stdin);    int num=1;    while(scanf("%d %d",&n,&t) && (n+t))    {        ans = 0;        memset(link,0,sizeof(link));        for(int i=0; i<t; ++i)        {            int a,b;            scanf("%d %d",&a,&b);            if(link[b][a] || a==b)            {                ans++;                continue;            }            //如果link[a][b]为1的话,就没必要再执行一次了            //不加这个判断,就会超时            if(link[a][b] == 0),                Update(a,b);        }        printf("%d. %d\n",num++,ans);    }        return 0;}
原创粉丝点击