Codeforce 893C Rumor (dfs)
2017-11-30 21:45
141 查看
you can find the problem in this link
given you N nodes and M edge , find the smallest number in each connect block and sum them up;
well , this is a simple dfs and I think I should use some data structure like union set but use vector to storage the edges and the time complexity is promising .
the mistake I made is make the question more complex and forget to left the mark. It should be think carefully.
given you N nodes and M edge , find the smallest number in each connect block and sum them up;
well , this is a simple dfs and I think I should use some data structure like union set but use vector to storage the edges and the time complexity is promising .
#include <bits/stdc++.h> #include <cstring> #define ll long long #define in : using namespace std; vector<int> edge[100001]; int c[100001],vis[1000001]; int n,m; void addedge(int u,int v) { edge[u].push_back(v); edge[v].push_back(u); } int dfs(int st) { int ans = c[st]; for(auto i in edge[st]) { if(vis[i]) continue; vis[i] = 1; ans = min(ans,dfs(i)); } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i = 1;i<=n;i++) { cin>>c[i]; } int u,v; for(int i = 0;i<m;i++) { cin>>u>>v; addedge(u,v); } ll ans = 0; memset(vis,0,sizeof(vis)); for(int i = 1;i<=n;i++) { if(!vis[i]) vis[i] = 1,ans += (ll)dfs(i); } cout<<ans<<endl; return 0; }
the mistake I made is make the question more complex and forget to left the mark. It should be think carefully.
相关文章推荐
- codeforce B. Preparing Olympiad (DFS+回溯)/(暴力状压)
- codeforce Pashmak and Buses(dfs枚举)
- codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
- CodeForces - 893C Rumor(并查集,DFS)
- codeforce 124B——全排列dfs——Permutations
- codeforce 776 D The Door Problem(DFS遍历)
- DFS J. Cleaner Robot Codeforce 589J(实例与分析)
- codeforce C. Valera and Elections (DFS)
- CodeForce 505C Mr. Kitayuta(DP+DFS)
- CodeForce VKcup C 树形dp
- codeforce Good Bye 2017 C. New Year and Curling 暴力
- CodeForce#190 Div1
- Codeforce 27B Tournament
- codeforce 486B
- HDU 1111 Secret Code DFS 解题报告
- codeforce 606C Sorting Railway Cars
- codevs1018 单词接龙(DFS)
- codeforces B. Fox And Two Dots (dfs)
- Code Force 21B Intersection
- codeforce #387 D. Winter Is Coming