P3144 关闭农场 并查集 反向

  

FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。

这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。

FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之前的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。

输入输出样例

输入样例#1: 复制

4 3
1 2
2 3
3 4
3
4
1
2

输出样例#1: 复制

YES
NO
YES
YES

题意:给出n个点和m条无向边 每次去掉一个点查看联通块是否为1

遵循先判断输出 然后去掉一个点的原则

并查集find1一定要优化  一开始就因为这个超时

因为并查集没有拆掉的操作  所以要倒着来  每次加上一个点  (比如拆到最后一个点的时候是一个点   倒着来补上第一个点的时候也是那一个点   所以是一样的)
因为奇特的读入输出模式  其实就是为了倒着来所准备的

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define LL long long
#define pb push_back
#define fi first
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
///////////////////////////////////
#define inf 0x3f3f3f3f
#define N 3000+5

int n,m;
int x[N],y[N];
int in[N];
int f[N];
int node[N];
int ans[N];
int find1(int x)
{
    return f[x]==x?x:(f[x]=find1(f[x]) );

}
void union1(int a,int b)
{
    int x=find1(a);
    int y=find1(b);
    if(x!=y)
        f[x]=y;
}

int main()
{
    RII(n,m);
    rep(i,1,n)
    f[i]=i;
    rep(i,1,m)
    {
        RII(x[i],y[i]);
    }
    rep(i,1,n)
    {
        RI(node[i]);
        in[node[i]]=0;
    }

    repp(i,n,1)
    {
        in[ node[i] ]=1;
        rep(j,1,m)
        if( in[ x[j] ]&&in[ y[j] ] )
            union1( x[j],y[j] );

        rep(j,1,n)//遍历n查看有几个联通块
        if(in[j]&&f[j]==j)
            ans[i]++;
    }
    rep(i,1,n)
    if(ans[i]==1)
        printf("YES\n");
    else
        printf("NO\n");
   return 0;
}

原文地址:https://www.cnblogs.com/bxd123/p/10569118.html

时间: 2024-08-29 17:56:26

P3144 关闭农场 并查集 反向的相关文章

P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver(并查集反向)

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NNN barns connected with MMM bidirectional paths between some pairs o

hdu 4496 并查集 反向并查集 水题 D-City

觉得这道题以后可以和优先队列结合起来 嗯 就是说依次去掉前n条路求连通块数量 处理的时候  只要每次merge发现父亲不相等 然后进到里面合并的时候 num-- wa了好几次是因为最后输出的时候开了点的数量大小的数组而不是操作数量数组 orz D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5334    Accepted

洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of  barns connected with  bidirectional paths between some pairs of barn

洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NN barns connected with MM bidirectional paths between some pairs of

luogu P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver解题报告

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NN barns connected with MM bidirectional paths between some pairs of

hdu4496 D-City(反向并查集)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496 D-City Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D

洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdio> #include<cstring> #define N 3030 using namespace std; int n,m,sumedge,cnt; int head[N],fa[N],q[N],ans[N],exit[N]; struct Edge{ int x,y,nxt; Edg

[USACO16OPEN]关闭农场Closing the Farm_Silver

题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime. The farm consists of NNN barns connected with MMM bidirectional paths between some pairs o

八中教室的灯加强版 【并查集】

本人水平有限,题解不到为处,请多多谅解 本蒟蒻谢谢大家观看 题目: 八中教室的灯加强版 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 79  Solved: 38[Submit][Status][Web Board] Description 八中一共有被用M条双向道路连接的N个教室(1<=N,M<=3000).为了关闭整个八中,master wen 计划每一次关闭掉一个教室.当一个教室被关闭了,所有的连接到这个教室的道路都会被关闭,而且再也不能够