原题链接
P1475
题目类型:
普
及
/
提
高
−
{\color{yellow} 普及/提高-}
普及/提高−
题目大意
输入 n n n和 n n n行 x , y , z x,y,z x,y,z,代表公司 x x x拥有公司 y y y百分之 z z z的股份。升序输出哪一个公司可以控制哪一个公司,不要输出自己控制自己的数据。注意, a a a公司可以控制 b b b公司,仅当:
- 公司 a = a= a=公司 b b b(就是一个公司)
- 公司 a a a占有 b b b公司 > 50 % >50\% >50%的股票
- 公司 a a a可以控制的公司占有 b b b公司总和 > 50 % >50\% >50%的股票
S a m p l e \mathbf{Sample} Sample I n p u t \mathbf{Input} Input
3
1 2 80
2 3 80
3 1 20
S a m p l e \mathbf{Sample} Sample O u t p u t \mathbf{Output} Output
1 2
1 3
2 3
解题思路
本题没有给出公司的数量,所以在输入时要注意处理一下公司的个数。
再以每一个公司做起点,
d
f
s
dfs
dfs遍历他可以控制的公司即可,注意每一个公司只能遍历一遍
上代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
int shares[110][110];
int have[110],cnt[110];
bool vis[110];
void dfs(int x)
{
if(vis[x]) return;
vis[x]=true;
for(int i=1; i<=n; i++)
{
if(i==x) continue;
cnt[i]+=shares[x][i];
if(cnt[i]>50)
{
have[i]=true;
dfs(i);
}
}
}
int main()
{
cin>>m;
for(int i=1; i<=m; i++)
{
int x,y,z;
cin>>x>>y>>z;
n=std::max(n,std::max(x,y));
shares[x][y]=z;
}
for(int i=1; i<=n; i++)
{
memset(have,0,sizeof(have));
memset(cnt,0,sizeof(cnt));
memset(vis,false,sizeof(vis));
dfs(i);
for(int j=1; j<=n; j++)
if(have[j]&&i!=j) cout<<i<<" "<<j<<endl;
}
return 0;
}
完美切题 ∼ \sim ∼