USACO-Controlling Companies

来源:互联网 发布:java实现微信公众号 编辑:程序博客网 时间:2024/06/05 14:49

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford at one point owned 12% of Mazda. It is said that a company A controls company B if at least one of the following conditions is satisfied:

Company A = Company B
Company A owns more than 50% of Company B
Company A controls K (K >= 1) companies denoted C1, …, CK with each company Ci owning xi% of company B and x1 + …. + xK > 50%.
Given a list of triples (i,j,p) which denote company i owning p% of company j, calculate all the pairs (h,s) in which company h controls company s. There are at most 100 companies.

Write a program to read the list of triples (i,j,p) where i, j and p are positive integers all in the range (1..100) and find all the pairs (h,s) so that company h controls company s.

PROGRAM NAME: concom

INPUT FORMAT

Line 1: n, the number of input triples to follow
Line 2..n+1: Three integers per line as a triple (i,j,p) described above.
SAMPLE INPUT (file concom.in)

3
1 2 80
2 3 80
3 1 20
OUTPUT FORMAT

List 0 or more companies that control other companies. Each line contains two integers that denote that the company whose number is the first integer controls the company whose number is the second integer. Order the lines in ascending order of the first integer (and ascending order of the second integer to break ties). Do not print that a company controls itself.
SAMPLE OUTPUT (file concom.out)

1 2
1 3
2 3

题意:给出你i公司在j公司占有p的股份,输出从属关系。

dfs。

代码:

/*ID:iam666PROG:concomLANG:C++*/#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>bool flag[105],own[105];int a[105][105]={},cnt[105],maxx;using namespace std;void dfs(int x){    if(flag[x])        return;    flag[x]=true;    for(int i=1;i<=maxx;i++)    {        cnt[i]+=a[x][i];        if(cnt[i]>50)        {            own[i]=true;            dfs(i);        }    }}int main (void){    freopen("concom.in","r",stdin);    freopen("concom.out","w",stdout);    int n,u,v,w;    cin>>n;    while(n--)    {        scanf("%d %d %d",&u,&v,&w);        a[u][v]+=w;        if(u>maxx)maxx=u;        if(v>maxx)maxx=v;    }    for(int i=1;i<=maxx;i++)    {        fill_n(cnt+1,maxx,0);        fill_n(flag+1,maxx,false);        fill_n(own+1,maxx,false);        dfs(i);        for(int j=1;j<=maxx;j++)            if(own[j]&&i!=j)                printf("%d %d\n",i,j);    }    return 0;}
0 0