• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
未来が好き!
博客园    首页    新随笔    联系   管理    订阅  订阅
Codeforces Round #375 (Div. 2) F. st-Spanning Tree

传送门

分析:构造题。可以这么想:先把s,t两个点去掉,把剩下的点先并查集合并。这样会出现N+2个集合:s, t, N个剩余集合。那么N个集合中先把只能与s或t中一个相连的连起来,如果这样已经超出了要求,那么就不能构造。剩余的既能和s又能和t相连的集合就按照不超过ds,dt这两个要求相连,可以则Yes,否则为No。这样有一个特殊情况:就是s或者t可能只有一条边连向t和s,不知道有没有说清楚,就是对于s只有s−t或者对于t只有s−t。这个需要在最后进行特判。

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   offcin        ios::sync_with_stdio(false)
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   slch          v<<1
#define   srch          v<<1|1
#define   sgetmid       int m = (l+r)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   pb            push_back
#define   fi            first
#define   se            second

const int    INF    = 0x3f3f3f3f;
const LL     INFF   = 1e18;
const double pi     = acos(-1.0);
const double inf    = 1e18;
const double eps    = 1e-9;
const LL     mod    = 1e9+7;
const int    maxmat = 10;
const ull    BASE   = 31;

/*****************************************************/

typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
int par[maxn];
int s, t, ds, dt;
bool inq[maxn];
int cons[maxn], cont[maxn];
std::vector<pii> vis;
std::vector<int> conn;
std::vector<int> G[maxn];
int N, M;

void init() {
    for (int i = 0; i <= N; i ++)
        par[i] = i;
}
int findpar(int x) {
    return par[x] = (par[x] == x ? x : findpar(par[x]));
}
void unite(int x, int y) {
    int px = findpar(x), py = findpar(y);
    if (px == py) return;
    // cout<<x<<" "<<y<<endl;
    vis.pb(mk(x, y));
    par[px] = py;
}
bool work() {
    init();
    for (int u = 1; u <= N; u ++) {
        if (u == s || u == t) continue;
        for (unsigned i = 0; i < G[u].size(); i ++) {
            int v = G[u][i];
            if (v == s || v == t) continue;
            unite(u, v);
        }
    }
    mem(inq, false);
    for (int u = 1; u <= N; u ++) {
        if (u == s || u == t) continue;
        int p = findpar(u);
        if (!inq[p]) {
            conn.pb(p);
            inq[p] = true;
        }
    }
    mem(cons, -1); mem(cont, -1);
    bool st = false;
    for (unsigned i = 0; i < G[s].size(); i ++) {
        int v = G[s][i];
        if (v == t) {st = true; continue;}
        int p = findpar(v);
        cons[p] = v;
    }
    for (unsigned i = 0; i < G[t].size(); i ++) {
        int v = G[t][i];
        if (v == s) {st = true; continue;}
        int p = findpar(v);
        cont[p] = v;
    }
    int anss = 0, anst = 0;
    for (unsigned i = 0; i < conn.size(); i ++) {
        int p = conn[i];
        if (cons[p] != -1 && cont[p] != -1) continue;
        if (cons[p] == -1 && cont[p] == -1) return false;
        if (cons[p] != -1 && cont[p] == -1) {
            unite(s, cons[p]);
            anss ++;
        }
        if (cons[p] == -1 && cont[p] != -1) {
            unite(t, cont[p]);
            anst ++;
        }
    }
    if (anss >= ds || anst >= dt) return false;
    ds -= anss, dt -= anst;
    bool flag = true;
    for (unsigned i = 0; i < conn.size(); i ++) {
        int p = conn[i];
        if (!(cons[p] != -1 && cont[p] != -1)) continue;
        if (flag) {
            ds --, dt --;
            unite(s, cons[p]);
            unite(t, cont[p]);
            flag = false;
        }
        else {
            if (ds) {
                ds --;
                unite(s, cons[p]);
            }
            else if (dt) {
                dt --;
                unite(t, cont[p]);
            }
            else return false;
        }
    }
    int ps = findpar(s), pt = findpar(t);
    if (ps != pt) {
        if (ds && dt && st) unite(s, t);
        else return false;
    }
    return true;
}

int main(int argc, char const *argv[]) {
    cin>>N>>M;
    for (int i = 0; i < M; i ++) {
        int u, v; cin>>u>>v;
        G[u].pb(v); G[v].pb(u);
    }
    cin>>s>>t>>ds>>dt;
    if (work()) {
        puts("Yes");
        for (unsigned i = 0; i < vis.size(); i ++)
            printf("%d %d\n", vis[i].fi, vis[i].se);
    }
    else puts("No");
    return 0;
}
posted on 2016-10-05 10:09  未来が好き!  阅读(143)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3