首页 > 技术文章 > Router Mesh

war1111 2020-11-26 22:33 原文

https://ac.nowcoder.com/acm/problem/211810

小米邀请赛第一场D题

#include<bits/stdc++.h>
#define LL long long
#define DB double
#define IL inline
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
using namespace std;

const int N=5e5+3;

int n,m,Q,tot,RT,top,cnt,num,Time,war_ans,dfn[N],low[N],sta[N],cut[N],mtx[N],du[N];
int vis[N],bel[N],pos[N],dep[N],head[N],lonely[N];
vector<int> vec[N];
struct edge{int x,y;}a[N];
struct EDGE{int next,to;}e[N<<1];

IL int gi() {
    int x=0,p=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') p=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
    return x*p;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}
struct node{
    int x;
    int y;
}s[N];
IL void New() {
    tot=0;
    memset(&e,0,sizeof(e));
    memset(head,0,sizeof(head));
    memset(vis,0,sizeof(vis));
}

IL void make(int x,int y) {
    e[++tot]=(EDGE){head[x],y},head[x]=tot;
    e[++tot]=(EDGE){head[y],x},head[y]=tot;
}

void tarjan(int x) {
    int i,k,y,num=0;
    dfn[x]=low[x]=++Time,sta[++top]=x;
    if(x==RT&&head[x]==0) {
        vec[++cnt].push_back(x);
        lonely[x]=1;
        return;
    }
    for(i=head[x];i;i=e[i].next)
        if(!dfn[y=e[i].to]) {
            tarjan(y),low[x]=min(low[x],low[y]);
            if(dfn[x]<=low[y]) {
                if(x!=RT||++num>1) cut[x]=1;
                vec[++cnt].push_back(x);
                do{
                    k=sta[top--]; 
                    vec[cnt].push_back(k);
                }while(k!=y);
            }
        }
        else low[x]=min(low[x],dfn[y]);
}

signed main(){
    scanf("%d%d",&n,&m);
    int i,j,x,y;
    for(i=1;i<=m;++i)
        scanf("%d%d",&s[i].x,&s[i].y),make(s[i].x,s[i].y);
    for(i=1;i<=n;++i)
        if(!dfn[i]) RT=i,tarjan(i),war_ans++;
    for(i=1;i<=n;++i)
        if(cut[i]) bel[i]=++cnt,mtx[cnt]=1;
    for(i=1,New();i<=cnt;++i)
        for(j=0;j<vec[i].size();++j)
            if(cut[x=vec[i][j]]) make(i,bel[x]),du[x]++;
            else bel[x]=i;
    For(i,1,n){
        o(war_ans+du[i]-cut[i]-lonely[i]);p(' ');
    }
    return 0;
}

 

推荐阅读