NKOJ P3696 vacation Customs House (disjoint-set)

Time limit: - MS   Space limitations: 65536 KB
Benchmark Description: Because of file input and output is too large, time 2000ms
Problem Description

Summer vacation, most of the students had gone home, only a small number of students are still in school competitions.
The school intends to gradually bring school buildings are closed. In order to reduce operating costs.
School N school buildings (numbered from 1 to N) are connected by M two-way road. Each closed down a building, the building is connected with all roads will be closed at the same time.
What the boss wants to know, each school shut down a building, whether the rest of the school buildings in the open state is connected. Connectivity refers to any two open floor can reach each other.
Note that there may initially not to be connected all the floors.

Input Format

The first line, a two integers N and M
Next M rows, each row two integers x and y, x represents, there is a direct path connected between the two buildings y.
Next N rows, each row an integer representing the number of a building. N entire row of numbers indicate the order of school buildings turn off.

Output Format

A total of N lines, each line is "YES" or "NO", if the rest of the floor is the YES output communication, otherwise the output NO.
Where the first line represents the beginning of a whole school building are reachable.
Wherein the first row i + 1 represents the i-th off school buildings, whether the rest of the building communication.

Sample input

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

Sample Output

YES
of NO
YES
YES

prompt

For 40% of the data 1≤N, M≤3000   

To 100% data 1≤N, M≤200000

 
I was retarded by kruskal to determine whether the road Unicom and autism
 
Analyzing can be set off in order to consider the reverse side seen deleted bordered 
1 plus each link block determines whether the current point is newly added may point in the set already combined if the combined ans ++;
If the original block Unicom -ans == 1 then the viable
Otherwise it will not work
 
code:
//
#include<bits/stdc++.h>
using namespace std;
int n,m;
#define maxnn 600000
int f[maxnn];
int ans[maxnn],shun[maxnn];
int sec=0;
int sta[maxnn],nex[maxnn],en[maxnn],tot,las[maxnn];
int mark[maxnn];

int  gf(int v)
{
    return f[v]==v? v:f[v]=gf(f[v]);
}
void add(int a,int b)
{
    en[++tot]=b;
    nex[tot]=las[a];
    las[a]=tot;
    sta[tot]=a;
}
int  judge(int v)
{
    int ans=0;
    mark[v]=1;
    for(int i=las[v];i;i=nex[i])
    {
        int y=en[i];
        if(mark[y])
        {
            if(gf(y)!=gf(v))
            {
                ans++;
                f[gf(y)]=gf(v);
            }
        }
    }
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);    
    int x,y;
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&shun[i]);
    }
    for(int i=n;i>=0;i--)
    {
        sec++;
        sec-=judge(shun[i]); 
        if(sec==1)
        ans[i]=1;
        else
        ans[i]=0;
    }    
    for(int i=1;i<=n;i++)
    {
        if(ans[i]==1)
        printf("YES\n"); 
        else
        printf("NO\n"); 
    }
    
    
    
    
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11260285.html