P6121 [USACO16OPEN] Closing the Farm G

纯粹的發表於2024-03-08

原題連結

題解

抽象化
抽象成點和邊,對於抹除一個點,判斷整個圖是否聯通
等價於建立一個點(被抹除點的前一個點),判斷這個點與周圍點相連後,累積合併次數是否等於點數減一

code

#define ll long long
#include<bits/stdc++.h>
using namespace std;
ll fa[200005];
ll finds(ll now){return (fa[now]=(now==fa[now])?now:finds(fa[now]));}
ll ans[200005]={0};
ll open[200005]={0},bing=0,shut[200005]={0};
vector<ll> G[200005];
inline void read(ll &x) {
	x = 0;
	ll flag = 1;
	char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')flag = -1;
        c = getchar();
    }
	while(c >= '0' && c <= '9') {
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	x *= flag;
}

inline void write(ll x)
{
    if(x < 0){
    	putchar('-');
		x = -x;
	}
    if(x > 9)
		write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    ll n,m;
    read(n);read(m);
    for(ll i=1;i<=n;i++)fa[i]=i;
    for(ll i=1;i<=m;i++)
    {
        ll x,y;
        read(x);read(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(ll i=1;i<=n;i++) read(shut[i]);
    for(ll i=n;i>=1;i--)
    {
        ll x=shut[i];
        open[x]=1;
        for(auto to:G[x]) if(open[to]&&finds(to)!=x&&(++bing)) fa[finds(to)]=x;
        ans[i]=(bing==n-i?1:0);
    }
    for(ll i=1;i<=n;i++) puts((ans[i]==1)?"YES":"NO");
    return 0;
}

相關文章