割点 割边

wa:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e6+5;
inline int read(){
    int a=0;bool b=1;char x=getchar();
    while(x<'0'||'9'<x){
        if(x=='-')b=0;
        x=getchar();
    }
    while('0'<=x && x<='9'){
        a=(a<<3)+(a<<1)+x-'0';
        x=getchar();
    }
    return b ? a : -a ;
} 
int first[maxn],next[maxm*2],to[maxm*2];
int edge_count=0;
inline void add(int x,int y){
    edge_count++;
    to[edge_count]=y;
    next[edge_count]=first[x];
    first[x]=edge_count;
}
int n,m,ans,Time;
bool g[maxn];
int pre[maxn],low[maxn];
void dfs(int root,int s){
    /*if(root==5){
        printf("!!\n");
    }*/
    pre[root]=low[root]=++Time;
    //printf("root%d pre%d\n",root,pre[root]);
    for(int i=first[root];i;i=next[i]){
        int v=to[i];
        //if(root==5)printf("v%d:\n",v);
        if(!pre[v]){
            dfs(v,s);
            low[root]=min(low[root],low[v]);
            //if(root==5)printf("%d\n",low[root]); 
            if(low[v]>=pre[root] && root!=s){
                g[root]=1;
                
                //printf("%d ",root);
            }
        }
        else low[root]=min(low[root],pre[v]);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1,u,v;i<=m;i++){
        u=read();v=read();
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++){
    if(pre[i])continue;
    //printf("*");
    int temp=0;
    
    for(int j=first[i];j;j=next[j]){
        temp++;
    }
    //printf("%d ",temp);
    if(temp>1)g[i]=1;
    
    dfs(i,i);
    }
    
    for(int i=1;i<=n;i++){
        ans+=g[i];
    //    printf("%d",g[i]);
    }
    //printf("%d\n",ans);
    return 0;    
}

wa2:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e6+5;
inline int read(){
    int a=0;bool b=1;char x=getchar();
    while(x<'0'||'9'<x){
        if(x=='-')b=0;
        x=getchar();
    }
    while('0'<=x&&x<='9'){
        a=(a<<3)+(a<<1)+x-'0';
        x=getchar();
    }
    return b ? a : -a ;
} 
int first[maxn],next[maxm*2],to[maxm*2];
int edge_count=0;
inline void add(int x,int y){
    edge_count++;
    to[edge_count]=y;
    next[edge_count]=first[x];
    first[x]=edge_count;
}
int n,m,ans,Time;
bool g[maxn];
int pre[maxn],low[maxn];
void dfs(int root){
    pre[root]=low[root]=++Time;
    for(int i=first[root];i;i=next[i]){
        int v=to[i];
        if(!pre[v]){
            dfs(v);
            low[root]=min(low[root],low[v]);
            if(low[root]>=pre[root]){
                g[root]=1;
            }
        }
        else low[root]=min(low[root],pre[v]);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1,u,v;i<=m;i++){
        u=read();v=read();
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++){
    if(pre[i])continue;
    int temp=0;
    for(int j=first[i];j;j=next[j]){
        temp++;
    }
    if(temp>1)g[i]=1;
    
    dfs(i);
    }
    
    for(int i=1;i<=n;i++){
        ans+=g[i];
    }
    printf("%d\n",ans);
    return 0;    
}

ac1:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e6+5;
inline int read(){
    int a=0;bool b=1;char x=getchar();
    while(x<'0'||'9'<x){
        if(x=='-')b=0;
        x=getchar();
    }
    while('0'<=x && x<='9'){
        a=(a<<3)+(a<<1)+x-'0';
        x=getchar();
    }
    return b ? a : -a ;
} 
int first[maxn],next[maxm*2],to[maxm*2];
int edge_count=0;
inline void add(int x,int y){
    edge_count++;
    to[edge_count]=y;
    next[edge_count]=first[x];
    first[x]=edge_count;
}
int n,m,ans,Time;
bool g[maxn];
int pre[maxn],low[maxn],temp;
void dfs(int root,int s){
    /*if(root==5){
        printf("!!\n");
    }*/
    pre[root]=low[root]=++Time;
    //printf("root%d pre%d\n",root,pre[root]);
    for(int i=first[root];i;i=next[i]){
        int v=to[i];
        //if(root==5)printf("v%d:\n",v);
        if(!pre[v]){
            dfs(v,s);
            low[root]=min(low[root],low[v]);
            if(root==s)temp++; 
            if(low[v]>=pre[root] && root!=s){
                g[root]=1;
                
                //printf("%d ",root);
            }
        }
        else low[root]=min(low[root],pre[v]);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1,u,v;i<=m;i++){
        u=read();v=read();
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++){
    if(pre[i])continue;
    //printf("*");
    temp=0;
    if(temp>1)g[i]=1;
    dfs(i,i);
    }
    
    for(int i=1;i<=n;i++){
        ans+=g[i];
    //    printf("%d",g[i]);
    }
    printf("%d\n",ans);
    return 0;    
}

ac2:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e6+5;
inline int read(){
    int a=0;bool b=1;char x=getchar();
    while(x<'0'||'9'<x){
        if(x=='-')b=0;
        x=getchar();
    }
    while('0'<=x && x<='9'){
        a=(a<<3)+(a<<1)+x-'0';
        x=getchar();
    }
    return b ? a : -a ;
} 
int first[maxn],next[maxm*2],to[maxm*2];
int edge_count=0;
inline void add(int x,int y){
    edge_count++;
    to[edge_count]=y;
    next[edge_count]=first[x];
    first[x]=edge_count;
}
int n,m,ans,Time;
bool g[maxn];
int pre[maxn],low[maxn];
void dfs(int root,int s){
    /*if(root==5){
        printf("!!\n");
    }*/
    pre[root]=low[root]=++Time;
    int flag=0;
    //printf("root%d pre%d\n",root,pre[root]);
    for(int i=first[root];i;i=next[i]){
        int v=to[i];
        //if(root==5)printf("v%d:\n",v);
        if(!pre[v]){
            dfs(v,s);
            low[root]=min(low[root],low[v]);
            if(low[v]>=pre[root]){
                flag++;
                if(root!=s || flag>1)g[root]=1;
                //printf("%d ",root);
            }
        }
        else low[root]=min(low[root],pre[v]);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1,u,v;i<=m;i++){
        u=read();v=read();
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++){
    if(pre[i])continue;
    //printf("*");
    
    dfs(i,i);
    }
    
    for(int i=1;i<=n;i++){
        ans+=g[i];
    //    printf("%d",g[i]);
    }
    printf("%d\n",ans);
    return 0;    
}

二:

ac

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
const int maxm=1e6+5;
inline int read(){
    int a=0;bool b=1;char x=getchar();
    while(x<'0'||'9'<x){
        if(x=='-')b=0;
        x=getchar();
    }
    while('0'<=x && x<='9'){
        a=(a<<3)+(a<<1)+x-'0';
        x=getchar();
    }
    return b ? a : -a ;
} 
struct edge{
    int a;
    int b;
    void get_end(int x,int y){
        if(x<y){
            a=x;b=y;
        }
        else{
            b=x;a=y;
        }
    }
}e[maxm];
inline bool cmp(const edge&x,const edge&y){
    return x.a < y.a || x.a == y.a && x.b < y.b; 
}
int first[maxn],next[maxm*2],to[maxm*2];
int edge_count=0;
inline void add(int x,int y){
    edge_count++;
    to[edge_count]=y;
    next[edge_count]=first[x];
    first[x]=edge_count;
}
int n,m,Time;
bool g[maxn];
int pre[maxn],low[maxn],temp;
void dfs(int root,int fa){
    /*if(root==5){
        printf("!!\n");
    }*/
    pre[root]=low[root]=++Time;
    //printf("root%d pre%d\n",root,pre[root]);
    for(int i=first[root];i;i=next[i]){
        int v=to[i];
        //if(root==5)printf("v%d:\n",v);
        if(!pre[v]){
            dfs(v,root);
            low[root]=min(low[root],low[v]);
           // if(root==s)temp++; 
            if(low[v]>pre[root]){
                e[++edge_count].get_end(root,v);
                //printf("%d ",root);
            }
        }
        else if(v!=fa)low[root]=min(low[root],pre[v]);
    }
}
int main()
{
    n=read();m=read();
    for(int i=1,u,v;i<=m;i++){
        u=read();v=read();
        add(u,v);add(v,u);
    }
    edge_count=0;
    for(int i=1;i<=n;i++){
    if(pre[i])continue;
    //printf("*");
    
    dfs(i,0);
    }
    sort(e+1,e+edge_count+1,cmp);
    for(int i=1;i<=edge_count;i++){
        printf("%d %d\n",e[i].a,e[i].b);
    }
    //printf("%d\n",ans);
    return 0;    
}

 

posted @ 2019-04-25 21:05  Tj1  阅读(121)  评论(0编辑  收藏  举报