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
题意:
给出i公司占有j公司p%的股份,当p>50时,表示i控制了j. (i能直接或间接地控制j)。
代码如下:
/*
LANG: C
PROG: concom
*/
#include<stdio.h>
int max;
int t[110][110], v[110][110];
void Set()
{
int i, j;
for (i = 1; i <= 100; i++)
{
for (j = 1; j <= 100; j++)
{
t[i][j] = 0;
v[i][j] = 0;
}
}
}
void Add(int x, int y)
{
int i;
for (i = 1; i <= max; i++)
{
if (x != i && v[x][i] <= 50)//当公司x没控制i公司时,才需要检查x是否能控制i。
{
v[x][i] += t[y][i];
if (v[x][i] > 50)
{
Add(x, i);
}
}
}
}
int main()
{
freopen("concom.in", "r", stdin);
freopen("concom.out", "w", stdout);
int i, j, k, p, n;
scanf("%d", &n);
Set();
max = 0;
for (i = 0; i < n; i++)
{
scanf("%d%d%d", &j, &k, &p);
if (j > max)
{
max = j;
}
if (k > max)
{
max = k;
}
if (j != k)//排除输入不符合题意目的的情况。即排除自己占自己的的公司
{
t[j][k] = p;
v[j][k] = p;
}
}
for (i = 1; i <= max; i++)
{
for (j = 1; j <= max; j++)
{
if (t[i][j] > 50)//当i占有j时,更新i占有j的邻接公司的股份。
{
Add(i, j);
}
}
}
for (i = 1; i <= max; i++)
{
for (j = 1; j <= max; j++)
{
if ( v[i][j] > 50)
{
printf("%d %d\n", i, j);
}
}
}
fclose(stdin);
fclose(stdout);
//system("pause");
return 0;
}