AcWing 2069. 网络分析
原题链接
困难
作者:
a_little
,
2024-03-25 22:03:31
,
所有人可见
,
阅读 5
#include<bits/stdc++.h>
using namespace std;
const int N=10000,M=N*2;
int n,m;
int h[N],e[M],w[N],ne[M],idx;
bool st[N];
void add(int a,int b){
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int v){
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(!st[j]){
st[j]=true;
w[j]+=v;
dfs(j,v);
}
}
if(h[u]==-1) w[u]+=v;
}
int main(){
memset(h, -1, sizeof h);
cin>>n>>m;
while(m--){
memset(st,0,sizeof st);
int op,a,b;
cin>>op>>a>>b;
if(op==1){
add(a,b);
add(b,a);
}
else{
dfs(a,b);
}
}
for(int i=1;i<=n;i++){
cout<<w[i]<<" ";
}
return 0;
}
AcWing《算法基础课》拼团优惠!https://www.acwing.com/activity/content/introduction/11/group_buy/205739/