AtCoder AGC038D Unique Path (图论)

Topic Link

https://atcoder.jp/contests/agc038/tasks/agc038_f

answer

orz zjr immortal approach
consider all \ (C_i = 0 \) two tips connected edge, so even after the completion of each block are in communication a tree
that can not appear in the same communication block \ (C_i = 1 \ ) prompt, then communication between different blocks can be connected to any side, but the communication between the two blocks to meet even only one side, but also in communication
with \ (C \) interconnecting blocks, then it would block the communication even between \ (m- (nc) \) edges
without \ (C_i = 1 \) prompt, it only requires \ (c-1 \ le m- (nc) \ le \ frac {c (c- 1)} {2} \)
if any, requires \ (\ max (3, c
) \ le m- (nc) \ le \ frac {c (c-1)} {2} \) time complexity \ (O (n) \)

Code

#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std;

inline int read()
{
    int x=0; bool f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
    if(f) return x;
    return -x;
}

const int N = 2e5;
struct Element
{
    int u,v,typ;
} a[N+3];
int uf[N+3];
int n,q,c; llong m;

int findfa(int u) {return uf[u]==u?u:uf[u]=findfa(uf[u]);}

int main()
{
    scanf("%d%lld%d",&n,&m,&q);
    for(int i=1; i<=n; i++) uf[i] = i;
    for(int i=1; i<=q; i++)
    {
        scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].typ); a[i].u++; a[i].v++;
        if(a[i].typ==0)
        {
            int uu = findfa(a[i].u),vv = findfa(a[i].v);
            if(uu!=vv) {uf[uu] = vv;}
        }
    }
    int cnt1 = 0; c = 0;
    for(int i=1; i<=n; i++) {if(i==findfa(i)) c++;}
    for(int i=1; i<=q; i++)
    {
        if(a[i].typ==1)
        {
            cnt1++;
            int uu = findfa(a[i].u),vv = findfa(a[i].v);
            if(uu==vv) {puts("No"); return 0;}
        }
    }
    llong l = cnt1?max(3ll,(llong)c):c-1ll,r = 1ll*c*(c-1ll)/2ll;
    if(m-(n-c)>=l && m-(n-c)<=r) {puts("Yes");}
    else {puts("No");}
    return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/11574014.html