70ptsT飞了求助(玄关)
查看原帖
70ptsT飞了求助(玄关)
1199534
ycy1124楼主2024/11/28 20:09

疑似是 O(nlogn)\mathcal{O}( n\log n ) 的,但是常数好像炸了,求大佬优化qwq

#include<bits/stdc++.h>
#define N 1000005
#define int long long
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
inline int rint(){
	register int x(0),f(1);
	char ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++);
	while(!(ch>='0'&&ch<='9'))
		ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++);
	while(ch>='0'&&ch<='9')
		x=x*10+(ch^48),ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++);
	return x*f;
}
vector<int>a[N];
int mid;
bool vis[N],bj[N],qwq[N];
int n,m,k;
struct Node{
    int p,w;
};
queue<Node>q;
bool Check;
inline void bfs(int p,int w){
    bj[p]=1;
    if(w==mid){
        return;
    }
    for(auto it:a[p]){
        if(!bj[it]){
            bj[it]=1;
            q.push({it,w+1});
        }
    }
}
inline void Dfs(int p,int fa){
    qwq[p]=1;
    if(Check){
        return;
    }
    for(auto it:a[p]){
        if(it==fa){
            continue;
        }
        if(qwq[it]){
            Check=1;
            break;
            
        }
        if(bj[it]){
            qwq[it]=1;
            Dfs(it,p);
        }
    }
    return;
}
inline bool check(){
    Check=0;
    for(int i=1;i<=n;i++){
        qwq[i]=0;
        bj[i]=0;
        if(vis[i]){
            q.push({i,0});
        }
    }
    while(!q.empty()){
        bfs(q.front().p,q.front().w);
        q.pop();
    }
    for(int i=n;i>=1;i--){
        if(qwq[i]||!bj[i]){
            continue;
        }
        Dfs(i,0);
        if(Check){
            return 1;
        }
    }
    return 0;
}
signed main(){
    n=rint(),m=rint(),k=rint();
    for(int i=1;i<=m;i++){
        int u=rint(),v=rint();
        a[u].push_back(v);
        a[v].push_back(u);
    }
    for(int i=1;i<=k;i++){
        int w;
        w=rint();
        vis[w]=1;
    }
    mid=n;
    if(!check()){
        cout<<"Poor D!";
        return 0;
    }
    int l=0,r=n,ans;
    while(l<=r){
        mid=((l+r)>>1);
        if(check()){
            ans=mid;
            r=mid-1;
        }
        else{
            ans=l=mid+1;
        }
    }
    cout<<ans;
    return 0;
}
2024/11/28 20:09
加载中...