您的位置:首页 > 其它

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 .

#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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs