ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces 723F st-Spanning Tree

2019-05-19 17:53:20  阅读:284  来源: 互联网

标签:723F const int LL Tree ans Spanning dt define


st-Spanning Tree

题解:

将除了s, t以外的点相联通的点缩成一个点。

然后将将这些点和分类, 

1. 只和s相连, 2 只和t相连 3.同时和s, t相连。

对于1 2来说将他们都连到对应的点上去。

对于3来说,则是能连s就连s, 能连t就连t。

为了保证s, t联通, 我们将第3种点的第一个和s t相连。 (注意处理  s与t直接相连的情况)。

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 2e5 + 100;
int n, m, u, v;
int s, t, ds, dt;
int pre[N], vis[N];
vector<int> vc[N];
vector<pll> ans;
void dfs(int col, int u){
    pre[u] = col;
    vis[u] = 1;
    for(int v : vc[u]){
        if(vis[v]) continue;
        ans.pb({u, v});
        dfs(col, v);
    }
}
int cnt[N];
int e[N][2];
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; ++i){
        scanf("%d%d", &u, &v);
        vc[u].pb(v); vc[v].pb(u);
    }
    scanf("%d%d%d%d", &s, &t, &ds, &dt);
    vis[s] = vis[t] = 1;
    pre[s] = pre[t] = n + 1;
    pre[n+1] = n+1;
    for(int i = 1; i <= n; ++i){
        if(vis[i]) continue;
        dfs(i, i);
    }
    for(int v : vc[s]){
        cnt[pre[v]] |= 1;
        e[pre[v]][0] = v;
    }
    for(int v : vc[t]){
        cnt[pre[v]] |= 2;
        e[pre[v]][1] = v;
    }
    vector<int> zzz;
    for(int i = 1; i <= n + 1; ++i){
        if(i != pre[i]) continue;
        if(cnt[i] == 1) {
            ds--;
            ans.pb({s, e[i][0]});
        }
        if(cnt[i] == 2){
            dt--;
            ans.pb({t, e[i][1]});
        }
        if(cnt[i] == 3) zzz.pb(i);
    }
    for(int i = 0; i < zzz.size(); ++i){
        v = zzz[i];
        if(!i){
            ds--, dt--;
            if(v == n + 1)
                ans.pb({t, s});
            else ans.pb({s, e[v][0]}), ans.pb({t, e[v][1]});
        }
        else {
            if(v == n+1) continue;
            if(ds > 0){
                ds--;
                ans.pb({s, e[v][0]});
            }
            else if(dt > 0){
                dt--;
                ans.pb({t, e[v][1]});
            }
            else {
                puts("No");
                return 0;
            }
        }
    }
    if(ds < 0 || dt < 0){
        puts("No");
        return 0;
    }
    puts("Yes");
    for(auto & it : ans){
        printf("%d %d\n", it.fi, it.se);
    }
    return 0;
}
View Code

 

标签:723F,const,int,LL,Tree,ans,Spanning,dt,define
来源: https://www.cnblogs.com/MingSD/p/10889981.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有