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)
31 2 802 3 803 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 21 32 3 一道简单的dfs题,把每一个公司都作为总公司,分别得出占有其他公司的股份。 代码如下:
/*ID: yizeng21PROB: concomLANG: C++*/#include#include #include 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