USACO Section 2.3 Controlling Companies(DFS)

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
题意:这里有 n 个公司,公司之间有一定的股份,如果 A 公司持有 B 公司的 50% 以上,则 A 公司可以控制 B 公司。另外被 A 控制的公司,他们所控制的公司 C 的股份之和如果大于 50%,则 A公司也可以控制 C 公司。输出所有的控制和被控制的公司。
分析:直接搜。对于每个公司进行一次DFS

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: concom
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<fstream>
#include<vector>
#include<cstdio>
#define MAXN 110

using namespace std;

struct ANS{int x,y;}ans[MAXN*MAXN];
struct edge{int v,w;};
vector<edge>map[MAXN];
int cnt,sum[MAXN];
bool visited[MAXN],flag[MAXN];

FILE *fin  = fopen ("concom.in", "r");
FILE *fout = fopen ("concom.out", "w");

void DFS(int u)
{
    if(visited[u]) return ;
    visited[u]=true;
    int v,w;
    vector<edge>::iterator it;
    for(it=map[u].begin();it!=map[u].end();it++)
    {
        v=it->v;w=it->w;
        sum[v]+=w;
        if(sum[v]>50)
        {
            flag[v]=true;
            DFS(v);
        }
    }
}

void work(int n)
{
    int u,v,i;cnt=0;
    for(u=1;u<=n;u++)
    {
        memset(sum,0,sizeof(sum));
        memset(flag,false,sizeof(flag));
        memset(visited,false,sizeof(visited));
        DFS(u);
        for(v=1;v<=n;v++)
        {
            if(flag[v]&&u!=v)
            {
                ans[cnt].x=u;ans[cnt].y=v;cnt++;
            }
        }
    }
    for(i=0;i<cnt;i++)
    {
        fprintf(fout,"%d %d\n",ans[i].x,ans[i].y);
    }
}

int main()
{
    int i,n,N,u;
    edge e;
    while(fscanf(fin,"%d",&n)==1)
    {
        N=0;
        for(i=0;i<MAXN;i++) map[i].clear();
        for(i=0;i<n;i++)
        {
            fscanf(fin,"%d%d%d",&u,&e.v,&e.w);
            map[u].push_back(e);
            N=max(N,u);
            N=max(N,e.v);
        }
        work(N);
    }
    return 0;
}

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/09/04/2670462.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值