您的位置:首页 > 其它

USACO-Controlling Companies

2016-06-12 13:50 281 查看
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:iam666
PROG:concom
LANG: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: