• 【专题复习2:图的遍历】1013、1021、1034、1076


    1013

    1013

    点击查看代码
    #include <bits/stdc++.h>
    
    using namespace std;
    vector<int> G[10010];
    bool vis[10010];
    int curpoint;
    void dfs(int index)
    {
        if(index==curpoint) return;
        vis[index]=true;
        for(int i=0;i<G[index].size();i++)
            if(!vis[G[index][i]])
                dfs(G[index][i]);
    }
    int main()
    {
        #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int n,m,k,a,b;
        cin>>n>>m>>k;
        for(int i=0;i<m;i++){
            cin>>a>>b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        for(int i=0;i<k;i++){
            cin>>curpoint;
            memset(vis,false,sizeof(vis));
            int block=0;
            for(int j=1;j<=n;j++){
                if(j!=curpoint&&vis[j]==false){
                    dfs(j);
                    block++;
                }
            }
            cout<<block-1<<endl;
        }
        return 0;
    }
    
    

    1021

    1021

    点击查看代码
    #include <bits/stdc++.h>
    
    using namespace std;
    vector<int> G[10010];
    bool vis[10010];
    int maxdepth;
    set<int> temp,ans;
    void dfs(int index,int depth)
    {
        if(depth>maxdepth){
            temp.clear();
            maxdepth=depth;
            temp.insert(index);
        }else if(depth==maxdepth)
            temp.insert(index);
        vis[index]=true;
        for(int i=0;i<G[index].size();i++)
            if(!vis[G[index][i]])
                dfs(G[index][i],depth+1);
    }
    int main()
    {
        #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int n,a,b,cnt=0,s1=0;
        cin>>n;
        for(int i=0;i<n-1;i++){
            cin>>a>>b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        for(int i=1;i<=n;i++){
            if(vis[i]==false){
                dfs(i,1);
                cnt++;
            }
        }
        if(cnt>=2)
            printf("Error: %d components\n",cnt);
        else{
            ans=temp;
            temp.clear();
            fill(vis,vis+10010,false);
            auto it=ans.begin();
            dfs(*it,1);
            for(auto it=temp.begin();it!=temp.end();it++)
                ans.insert(*it);
            for(auto it=ans.begin();it!=ans.end();it++)
                cout<<*it<<endl;
        }
        return 0;
    }
    
    

    1034

    1034

    点击查看代码
    #include <bits/stdc++.h>
    
    using namespace std;
    int G[2010][2010],weight[2010],k;
    bool vis[2010];
    map<string,int> strToInt,ans;
    map<int,string> intToStr;
    int id=1;
    int change(string s)
    {
        if(strToInt[s]==0){
            strToInt[s]=id;
            intToStr[id]=s;
            return id++;
        }else{
            return strToInt[s];
        }
    }
    void dfs(int u,int &head,int &numMember,int &totalweight)
    {
        vis[u]=true;
        numMember++;
        if(weight[u]>weight[head])
            head=u;
        for(int v=1;v<id;v++){
            if(G[u][v]>0){
                totalweight+=G[u][v];
                G[u][v]=G[v][u]=0;
                if(vis[v]==false)
                    dfs(v,head,numMember,totalweight);
            }
        }
    }
    void dfsTrave()
    {
        for(int i=1;i<id;i++){
            if(vis[i]==false){
                int head=i,numMember=0,totalweight=0;
                dfs(i,head,numMember,totalweight);
                if(numMember>2&&totalweight>k)
                    ans[intToStr[head]]=numMember;
            }
        }
    }
    int main()
    {
        #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int n,a,b,w;
        string s1,s2;
        cin>>n>>k;
        for(int i=0;i<n;i++){
            cin>>s1>>s2>>w;
            int a=change(s1);
            int b=change(s2);
            weight[a]+=w;
            weight[b]+=w;
            G[a][b]+=w;
            G[b][a]+=w;
        }
        dfsTrave();
        cout<<ans.size()<<endl;
        for(auto it=ans.begin();it!=ans.end();it++)
            cout<<it->first<<" "<<it->second<<endl;
        return 0;
    }
    
    

    1076

    1076

    点击查看代码
    #include <bits/stdc++.h>
    
    using namespace std;
    struct node
    {
        int id,layer;
    };
    int n,l;
    vector<int> v[1010];
    int bfs(node tnode)
    {
        queue<node> q;
        q.push(tnode);
        bool vis[1010]={false};
        vis[tnode.id]=true;
        int cnt=0;
        while(!q.empty()){
            node top=q.front();
            q.pop();
            int topid=top.id;
            for(int i=0;i<v[topid].size();i++){
                int nextid=v[topid][i];
                if(vis[nextid]==false&&top.layer<l){
                    node next={nextid,top.layer+1};
                    q.push(next);
                    vis[next.id]=true;
                    cnt++;
                }
            }
        }
        return cnt;
    }
    int main()
    {
        #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int num,temp;
        cin>>n>>l;
        for(int i=1;i<=n;i++){
            cin>>num;
            for(int j=0;j<num;j++){
                cin>>temp;
                v[temp].push_back(i);
            }
        }
        int k,tid;
        cin>>k;
        for(int i=0;i<k;i++){
            cin>>tid;
            node tnode={tid,0};
            cout<<bfs(tnode)<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    django第10天(聚合查询,常用字段)
    django第9天(多表操作)
    django第8天(在测试文件中运行django项目|单表操作)
    django第七天(模板的复用性,include标签和母版)
    django第六天(模板相关,过滤器和标记)
    SparkCore2
    SparkCore
    SQL
    Spark基础
    使用Observer实现HBase到Elasticsearch的数据同步
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15940927.html
Copyright © 2020-2023  润新知