Stock Chase
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.
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.
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.
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
//题意:有很多个公司互相收购股份,若A收购了B的股份,B收购了C的股份,那么如果C收购了A的股份就是非法的,意思就是给的图里不能出现环,若出现了环,就不加这条边,并且记录+1,最后输出记录。(解释的不清楚,最好自己看下题目— —!)
//思路:用一个二维数组存放图,注意,是有可能出现输入的时候a==b的情况的,这种情况直接记录+1就好了(因为这就是一个自环啊)。然后重点就在图的更新那里了,如果把a->b插入到图中(即map[a][b]=1),如果还有个i->a的边,那么我们把map[i][b]设置成1,因为如果再有一条map[b][i]的话就构成环了,我们先在这把map[i][b]=1,然后在主函数里弄个if (map[b][a]==1)就记录+1,这样的话如果后面输入了b,i,那么那么直接就记录+1了,不会再去更新了。
不理解看图,如果已有i->a,a->b,那么如果下次输入的是b->i的话就构成环了(非法),所以我们就自己加了条i->b,如果下次输入的是b,i,那么因为map[i][b]==1,直接记录+1。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 234 + 10;
int num;
int n, t;
int map[MAX][MAX];
void update(int a, int b)
{
map[a][b] = 1;
for (int i = 1; i <= n; i++)
{
if (map[i][a])
map[i][b] = 1;
}
for (int i = 1; i <= n; i++)
{
if (map[b][i])
{
map[a][i] = 1;
for (int j = 1; j <= n; j++)
{
if (map[j][b])
map[j][i] = 1;
}
}
}
}
int main()
{
int Case = 1;
while (scanf("%d%d", &n, &t), n || t)
{
int a, b;
int ans = 0;
memset(map, 0, sizeof(map));
for (int i = 0; i < t; i++)
{
scanf("%d%d", &a, &b);
if (a == b)
ans++;
else if (map[b][a])
ans++;
else if (map[a][b] == 0)
update(a, b);
}
printf("%d. %d\n", Case++, ans);
}
return 0;
}