usaco-Section 2.3-Controlling Companies

Controlling Companies

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford owns 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

一道简单的dfs题,把每一个公司都作为总公司,分别得出占有其他公司的股份。

代码如下:
/*
ID: yizeng21
PROB: concom
LANG: C++
*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
bool vist[1000];
int n;
int chun[1000][1000];
int q[1000][1000];
void dfs(int pos,int s){
    for(int i=1;i<=100;i++)
        if(chun[pos][i]!=0){
            if(vist[i]==1)continue;
            q[s][i]+=chun[pos][i];
            if(q[s][i]>50){
                vist[i]=1;
                dfs(i,s);
            }
        }
}
struct hh{
    int x,y;
}w[10000];
bool cmp(hh i,hh j){
    if(i.x<j.x)return 1;
    if(i.x==j.x){
        if(i.y<j.y)return 1;
        return 0;
    }
    return 0;
}
int main(){
    freopen("concom.in","r",stdin);
    freopen("concom.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        chun[a][b]=c;
    }
    int tot=0;
    for(int i=1;i<=100;i++){
        memset(q,0,sizeof(q));
        memset(vist,0,sizeof(vist));
        dfs(i,i);
        for(int j=1;j<=100;j++)
            if(vist[j]==1){
                w[++tot].x=i;
                w[tot].y=j;
            }
    }
    sort(w+1,w+tot+1,cmp);
    for(int i=1;i<=tot;i++)
        if(w[i].x!=w[i].y)
            printf("%d %d\n",w[i].x,w[i].y);
}

 

转载于:https://www.cnblogs.com/buffms/p/5155679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值