zoukankan      html  css  js  c++  java
  • st-Spanning Tree

    st-Spanning Tree
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an undirected connected graph consisting of n vertices and m edges. There are no loops and no multiple edges in the graph.

    You are also given two distinct vertices s and t, and two values ds and dt. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex s doesn't exceed ds, and the degree of the vertex t doesn't exceed dt, or determine, that there is no such spanning tree.

    The spanning tree of the graph G is a subgraph which is a tree and contains all vertices of the graph G. In other words, it is a connected graph which contains n - 1 edges and can be obtained by removing some of the edges from G.

    The degree of a vertex is the number of edges incident to this vertex.

    Input

    The first line of the input contains two integers n and m (2 ≤ n ≤ 200 000, 1 ≤ m ≤ min(400 000, n·(n - 1) / 2)) — the number of vertices and the number of edges in the graph.

    The next m lines contain the descriptions of the graph's edges. Each of the lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v) — the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.

    The last line contains four integers stdsdt (1 ≤ s, t ≤ ns ≠ t1 ≤ ds, dt ≤ n - 1).

    Output

    If the answer doesn't exist print "No" (without quotes) in the only line of the output.

    Otherwise, in the first line print "Yes" (without quotes). In the each of the next (n - 1) lines print two integers — the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.

    You can output edges in any order. You can output the ends of each edge in any order.

    If there are several solutions, print any of them.

    Examples
    input
    3 3
    1 2
    2 3
    3 1
    1 2 1 1
    output
    Yes
    3 2
    1 3
    input
    7 8
    7 4
    1 3
    5 4
    5 7
    3 2
    2 4
    6 1
    1 2
    6 4 1 4
    output
    Yes
    1 3
    5 7
    3 2
    7 4
    2 4
    6 1
    分析:根据贪心思想,先把不含s,t联通的联通块连上;
       然后把和s相连却不和t相连的联通块加入s,把和t相连却不和s相连的联通块加入t;
       然后对于和s和t都相连的联通块依次判断加入即可,最后看s和t是否联通及s和t是否直连即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    const int maxn=4e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,s,ds,dt,vis[maxn],pos1[maxn],pos2[maxn],cnt,ans1,ans2;
    vi e[maxn];
    bool f1[maxn],f2[maxn],ok,flag,ca;
    vector<pii>ans;
    void dfs(int now)
    {
        vis[now]=cnt;
        for(int x:e[now])
        {
            if(x!=s&&x!=t&&!vis[x])
            {
                ans.pb(mp(now,x));
                dfs(x);
            }
            else if(x==s)
            {
                f1[cnt]=true;
                pos1[cnt]=now;
            }
            else if(x==t)
            {
                f2[cnt]=true;
                pos2[cnt]=now;
            }
        }
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&j,&k);
            e[j].pb(k),e[k].pb(j);
        }
        scanf("%d%d%d%d",&s,&t,&ds,&dt);
        rep(i,1,n)
        {
            if(i!=s&&i!=t&&!vis[i])cnt++,dfs(i);
        }
        rep(i,1,cnt)
        {
            if(f1[i]&&!f2[i])ans.pb(mp(s,pos1[i])),ans1++;
            else if(!f1[i]&&f2[i])ans.pb(mp(t,pos2[i])),ans2++;
        }
        if(ans1>=ds||ans2>=dt)puts("No");
        else
        {
            ok=true;
            rep(i,1,cnt)
            {
                if(f1[i]&&f2[i])
                {
                    flag=true;
                    if(flag&&ok)
                    {
                        ans.pb(mp(s,pos1[i])),ans1++;
                        ans.pb(mp(t,pos2[i])),ans2++;
                        ok=false;
                    }
                    else
                    {
                        if(ans1<ds)ans.pb(mp(s,pos1[i])),ans1++;
                        else if(ans2<dt)ans.pb(mp(t,pos2[i])),ans2++;
                        else
                        {
                            flag=false;
                            break;
                        }
                    }
                }
            }
            for(int x:e[s])if(x==t){ca=true;break;}
            if(!flag&&ca&&ans1<ds&&ans2<dt)flag=true,ans.pb(mp(s,t));
            if(!flag)puts("No");
            else
            {
                puts("Yes");
                for(pii x:ans)printf("%d %d
    ",x.fi,x.se);
            }
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    arcgis server adf java
    JBoss4.2.3下载地址
    关心
    [转载]通过Arcgis Server向MXD中添加图层
    arcgis server问题总结
    想和做
    无敌
    [转载]通过Arcgis Server将某一图层从MXD中…
    Error retrieving "feature.xml".
    360很强大
  • 原文地址:https://www.cnblogs.com/dyzll/p/5931625.html
Copyright © 2011-2022 走看看