zoukankan      html  css  js  c++  java
  • POJ 2942

    坑爹题

    圆桌骑士可以有好多桌子就算了,骑士可以分身???同时在好多桌子???

    害得我卡了一个小时。。。

    正解:双联通分量,交叉染色判二分图!!

    结束

    (貌似双联通分量不需要开个栈维护边)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<queue>
    using namespace std;
    const int Maxn=1010,Maxm=1000010;
    struct edge{
    int v,nxt;
    }e[Maxm*2];
    int n,m,tot,ans,top,head[Maxn],dfn[Maxn],low[Maxn],col[Maxn],st[2*Maxm];
    bool hate[Maxn][Maxn],vis[Maxn],cntd[Maxn],sta[Maxn];
    int q[Maxn];
    void addedge(int u,int v){
    e[tot].v=v;
    e[tot].nxt=head[u];
    head[u]=tot++;
    }
    int count(){
    int i,sum=0;
    for(i=0;i<n;i++)if(sta[i]&&!cntd[i]){
    cntd[i]=1;
    sum++;
    }
    return sum;
    }
    int judge(int s){
    int front=0,tail=0;
    memset(col,-1,sizeof(col));
    q[tail++]=s;
    col[s]=0;
    while(front!=tail){
    int u=q[front++];
    for(int i=head[u];~i;i=e[i].nxt){
    int v=e[i].v;
    if(!sta[v])continue;
    if(~col[v]&&col[v]==col[u])return count();
    if(col[v]==-1){
    q[tail++]=v;
    col[v]=(col[u]^1);
    }
    }
    }
    return 0;
    }
    void tarjan(int depth,int f,int u){
    dfn[u]=low[u]=depth;
    vis[u]=1;
    st[++top]=u;
    int i;
    for(i=head[u];~i;i=e[i].nxt){
    int v=e[i].v;
    if(v==f)continue;
    if(vis[v]){low[u]=min(low[u],dfn[v]);continue;}
    tarjan(depth+1,u,v);
    low[u]=min(low[u],low[v]);
    if(low[v]==depth){
    memset(sta,0,sizeof(sta));
    while(st[top]!=u){
    sta[st[top--]]=1;
    }sta[st[top]]=1;
    ans+=judge(u);
    }
    }
    if(low[u]==depth)top--;
    }
    int main(){
    while(scanf("%d%d",&n,&m)){
    if(!n&&!m)break;
    tot=0;
    memset(hate,0,sizeof(hate));
    memset(cntd,0,sizeof(cntd));
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    top=0;
    int i,j;
    for(i=0;i<m;i++){
    int a,b;
    scanf("%d%d",&a,&b);
    a--,b--;
    hate[a][b]=hate[b][a]=1;
    }
    for(i=0;i<n;i++)for(j=0;j<n;j++)if(i!=j&&!hate[i][j])addedge(i,j);
    ans=0;
    for(i=0;i<n;i++)if(!vis[i])tarjan(0,-1,i);
    printf("%d ",n-ans);
    }
    return 0;
    }

  • 相关阅读:
    Delphi 10.3.3解决Android 11闪退
    QuickCore
    Delphi 10.4.1使用传统代码提示方案
    LINUX SHELL条件判断
    C#程序集使用强名字(Strong Name)签名/强名称签名
    ASP.NET Core环境变量和启动设置的配置教程
    ASP.NET Core MVC获取请求的参数方法示例
    Fluentvalidation的基本使用
    netstat & crontab
    Linux/Centos下多种方法查看系统block size大小
  • 原文地址:https://www.cnblogs.com/blogoflyn/p/10896215.html
Copyright © 2011-2022 走看看