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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/* 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; }