Codeforce 893C Rumor (dfs)

来源:互联网 发布:战斗妖精雪风小说淘宝 编辑:程序博客网 时间:2024/06/05 15:19

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.

原创粉丝点击