Hackerrank > 101 Hack 49 > Summing the Path Weights Between Nodes
类似拓扑排序的思想,每次从最末端的节点收缩。记录收缩过程中每个节点的红点和黑点的数量,计算每条边被使用的次数,复杂度O(2*N)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<cmath> #include<set> #include<stack> #define ll long long #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define cls(name,x) memset(name,x,sizeof(name)) using namespace std; const int inf=1<<28; const int maxn=1e5; const int maxm=110; const int mod=1e9+7; const double pi= acos (-1.0); int n; struct node { int to; ll val; }; vector<node> edge[maxn]; //邻接表 int sum_red,sum_black; struct vex { int red,black; int inc; //入度 }v[maxn]; ll ans; void solve() { int vis[maxn]; memset (vis,0, sizeof (vis)); queue< int > Q; for ( int i=0;i<n;i++) if (v[i].inc==1) Q.push(i); while (!Q.empty()) { int temp=Q.front(); Q.pop(); vis[temp]=1; for ( int i=0;i<edge[temp].size();i++) { int x=edge[temp][i].to; if (vis[x]==0) { ans+=edge[temp][i].val*((sum_red-v[temp].red)*v[temp].black+v[temp].red*(sum_black-v[temp].black)); v[x].red+=v[temp].red; v[x].black+=v[temp].black; v[x].inc--; if (v[x].inc==1) Q.push(x); } } } } int main() { //freopen("in.txt","r",stdin); while (~ scanf ( "%d" ,&n)) { ans=0;sum_red=sum_black=0; for ( int i=0;i<n;i++) {v[i].inc=0;} for ( int i=0;i<n;i++) edge[i].clear(); for ( int i=0;i<n;i++) { int a; scanf ( "%d" ,&a); if (a==0) { v[i].red=1; v[i].black=0; } else { v[i].red=0; v[i].black=1; } sum_red+=v[i].red; sum_black+=v[i].black; } for ( int i=0;i<n-1;i++) { int a,b,c; scanf ( "%d %d %d" ,&a,&b,&c); a--;b--; v[a].inc++; v[b].inc++; node t; t.to=b; t.val=c; edge[a].push_back(t); t.to=a; edge[b].push_back(t); } solve(); printf ( "%lld\n" ,ans); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步