(传递闭包) hdu 3357

Stock Chase

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m;
bool mp[300][300];
void update(int x,int y)
{
    mp[x][y]=true;
    for(int i=1;i<=n;i++)
    {
        if(mp[i][x])
            mp[i][y]=true;
    }
    for(int i=1;i<=n;i++)
    {
        if(mp[y][i])
        {
            mp[x][i]=true;
            for(int j=1;j<=n;j++)
            {
                if(mp[j][y])
                    mp[j][i]=true;
            }
        }
    }
}
int main()
{
    int x,y,cnt=0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        int ans=0;
        memset(mp,0,sizeof(mp));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            if(x==y) ans++;
            else if(mp[y][x]) ans++;
            else if(!mp[x][y])
            {
                update(x,y);
            }
        }
        printf("%d. %d\n",++cnt,ans);
    }
    return 0;
}

  

时间: 2024-12-25 01:34:50

(传递闭包) hdu 3357的相关文章

HDU 3357

http://acm.hdu.edu.cn/showproblem.php?pid=3357 给出公司间的控股关系,问有多少组不合法数据,自己控股自己不合法,a控股b,b控股c,则a控股c 其实就是找环,加一条边如果出现环ans++,但是每次搜一遍有没有环会tle.此处用邻接矩阵处理,如果a要控股b,则控股a的公司都控股b,并且a控股的公司都被控股a的控股.对于b的分析同理,此题有大量重复数据,需要去掉 #include <iostream> #include <cstdio> #

hdu 1704 Rank(floyd传递闭包)

题目链接:hdu 1704 Rank 题意: 有n个人,m场比赛,a与b打,每场都是awin,问不能确定其中两个人的win情况数. 题解: floyd传递闭包,这里我用bitset优化了一下. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 bitset<501>a[501]; 5 6 int n,m,t; 7 8 int main(){ 9

HDU 1704 Rank【传递闭包】

解题思路:给出n个选手,m场比赛,问不能判断胜负的询问最多有多少种 用传递闭包即可 但是如果直接用3重循环会超时 在判断d[i][j]=d[i][k]||d[k][j]是否连通的时候 可以加一个if语句判断一下d[i][k]是否为1,为1再进行第三重循环,不为1则不进行第三次循环 反思:例如询问 3和1,1和3是相同的情况,所以最后判断的时候,只需要判断上三角矩阵即可. Rank Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

HDU 3081--【二分图 &amp;&amp; 传递闭包 &amp;&amp; 完美匹配次数 &amp;&amp; 经典】

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2794    Accepted Submission(s): 938 Problem Description Presumably, you all have known the question of stable marriage match. A

HDU 5036 Explosion (传递闭包+bitset优化)

<题目链接> 题目大意: 一个人要打开或者用炸弹砸开所有的门,每个门后面有一些钥匙,一个钥匙对应一个门,告诉每个门里面有哪些门的钥匙.如果要打开所有的门,问需要用的炸弹数量为多少. 解题分析:因为许多门和他们之后的钥匙可能形成闭包的关系,所以,对于所有的闭包而言,只需要炸毁其中的一个门,就可以用其后面的钥匙打开闭包中至少一扇另外的门,一次类推.所以,假设闭包中包含$num$扇门,用炸弹打开闭包中任意一扇门的概率就为:$1/num$(因为炸毁每个闭包的概率为1,即每个闭包必然需要一枚炸弹).所有

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU 1704 Rank

Rank Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 170464-bit integer IO format: %I64d      Java class name: Main there are N ACMers in HDU team.ZJPCPC Sunny Cup 2007 is coming, and lcy want to select some

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116