USACO SECTION 2.3 Controlling Companies

本文探讨了企业间通过股权关系实现控制的方法,包括直接控股超过50%和间接控股累计超过50%的情况。通过输入企业间的持股比例,程序能够计算并输出所有存在控制关系的企业对。实例演示了如何利用输入数据,通过算法识别控制关系,并生成相应的输出结果。
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
ID: conicoc1
LANG: C
TASK: concom
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int Com[101][101][2];
int N;
int flag[101][101];

void Update(int i,int j)
{
	int k;
	flag[i][j]=1;
	for(k=1;k<=100;k++){
		Com[i][k][1]+=Com[j][k][0];
		if((Com[i][k][0]+Com[i][k][1]>50)&&flag[i][k]==0){
			Update(i,k);
		}
	}
}

void check()
{
	int i,j;
	for(i=1;i<=100;i++){
		for(j=1;j<=100;j++){
			if(i==j)
				continue;
			if((Com[i][j][0]+Com[i][j][1])>50&&flag[i][j]==0){
				Update(i,j);
			}
		}
	}
}
int main()
{
	FILE *fin,*fout;
	fin=fopen("concom.in","r");
	fout=fopen("concom.out","w");
	memset(Com,0,sizeof(Com));
	memset(flag,0,sizeof(flag));
	
	fscanf(fin,"%d",&N);
	
	int i,j,k,t;
	int compA,compB,cent;
	for(i=1;i<=N;i++){
		fscanf(fin,"%d %d %d",&compA,&compB,¢);
		Com[compA][compB][0]=cent;	
	}
	
	check();
	
	for(i=1;i<=100;i++){
		for(j=1;j<=100;j++){
			if(i==j)
				continue;
			if((Com[i][j][0]+Com[i][j][1])>50){
				fprintf(fout,"%d %d\n",i,j);
			}
		}
	}
        return 0;
}


  感觉是一个很抽象的DFS,一开始没有发现

 就是在每次更新A控制B之后,B的控股加到A的间接控股上,此时需要判定A是否控制了某公司

一开始没有用一个数组存放是否A已经控制了B的标记

思考循环结束的问题那里楞了好久。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值