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:nealgav1PROG:concomLANG:C++*/#include<fstream>#include<cstring>using namespace std;const int mm=889;ifstream cin("concom.in");ofstream cout("concom.out");int map[mm][mm],cnt[mm];bool s[mm],flag[mm];void dfs(int x,int n){ if(flag[x])return; flag[x]=1; for(int i=1;i<=n;i++) { cnt[i]+=map[x][i]; if(cnt[i]>50) { s[i]=1; dfs(i,n); } }}int main(){ int m,a,b,c,n=0; cin>>m; memset(map,0,sizeof(map)); for(int i=0;i<m;i++) { cin>>a>>b>>c; map[a][b]+=c; if(a>n)n=a;if(b>n)n=b; } for(int i=1;i<=n;i++) { memset(s,0,sizeof(s)); memset(flag,0,sizeof(flag)); memset(cnt,0,sizeof(cnt)); dfs(i,n); for(int j=1;j<=n;j++) if(s[j]&&j!=i) cout<<i<<" "<<j<<"\n"; }}
思路:一个公司遍历搜遍它的所有控股公司,然后将其输出,在重复进行第二个公司
USER: Neal Gavin Gavin [nealgav1] TASK: concom LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 6432 KB] Test 2: TEST OK [0.000 secs, 6432 KB] Test 3: TEST OK [0.000 secs, 6432 KB] Test 4: TEST OK [0.000 secs, 6432 KB] Test 5: TEST OK [0.000 secs, 6432 KB] Test 6: TEST OK [0.000 secs, 6432 KB] Test 7: TEST OK [0.000 secs, 6432 KB] Test 8: TEST OK [0.000 secs, 6432 KB] Test 9: TEST OK [0.022 secs, 6432 KB]
Controlling Companies
Russ Cox
The method used here to solve the problem is as follows. We keep track of which companies control which other companies, and every time we hear that so and so owns this much percent of so and so, we update our information.
The array "owns" keeps track of how much of company j is owned by company i, whether directly or via controlled companies. The array "controls" keeps track of which companies are controlled by which other companies.
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define NCOM 101int owns[NCOM][NCOM]; /* [i,j]: how much of j do i and its controlled companies own? */int controls[NCOM][NCOM]; /* [i, j]: does i control j? *//* update info: now i controls j */voidaddcontroller(int i, int j){ int k; if(controls[i][j]) /* already knew that */ return; controls[i][j] = 1; /* now that i controls j, add to i's holdings everything from j */ for(k=0; k<NCOM; k++) owns[i][k] += owns[j][k]; /* record the fact that controllers of i now control j */ for(k=0; k<NCOM; k++) if(controls[k][i]) addcontroller(k, j); /* if i now controls more companies, record that fact */ for(k=0; k<NCOM; k++) if(owns[i][k] > 50) addcontroller(i, k);}/* update info: i owns p% of j */voidaddowner(int i, int j, int p){ int k; /* add p% of j to each controller of i */ for(k=0; k<NCOM; k++) if(controls[k][i]) owns[k][j] += p; /* look for new controllers of j */ for(k=0; k<NCOM; k++) if(owns[k][j] > 50) addcontroller(k, j);}voidmain(void){ FILE *fin, *fout; int i, j, n, a, b, p; fin = fopen("concom.in", "r"); fout = fopen("concom.out", "w"); assert(fin != NULL && fout != NULL); for(i=0; i<NCOM; i++) controls[i][i] = 1; fscanf(fin, "%d", &n); for(i=0; i<n; i++) { fscanf(fin, "%d %d %d", &a, &b, &p); addowner(a, b, p); } for(i=0; i<NCOM; i++) for(j=0; j<NCOM; j++) if(i != j && controls[i][j]) fprintf(fout, "%d %d\n", i, j); exit(0);}