hit 1917 && hdu 1814 Peaceful Commission

模板题。hdu 1814要求输出字典序最小的。这样很麻烦,但是可以暴力。

不用加文件读入输出。

hit 1917

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn=16005;
const int maxm=200005;
int head[maxn],next[maxm],to[maxm];
int dfn[maxn],low[maxn],stk[maxn],scc[maxn],ind[maxn],vis[maxn];
int color[maxn],f[maxn];
int tot,top,cnt,id;
vector<int> dag[maxn];
void addEdage(int u,int v)
{
    next[tot]=head[u],to[tot]=v,head[u]=tot++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(ind,0,sizeof(ind));
    memset(color,0,sizeof(color));
    tot=top=cnt=id=0;
}
void tarjan(int v)
{
    dfn[v]=low[v]=++cnt;
    vis[v]=1;
    stk[++top]=v;
    for(int i=head[v];i!=-1;i=next[i])
    {
        int u=to[i];
        if(!dfn[u])
        {
            tarjan(u);
            low[v]=min(low[v],low[u]);
        }
        else if(vis[u])
            low[v]=min(low[v],dfn[u]);
    }
    if(low[v]==dfn[v])
    {
        id++;
        while(true)
        {
            int u=stk[top--];
            vis[u]=0;
            scc[u]=id;
            if(u==v)break;
        }
    }
}
void buildDag(int n)
{
    for(int u=0;u<2*n;u++)
        for(int i=head[u];i!=-1;i=next[i])
        {
            int v=to[i];
            if(scc[v]!=scc[u])
            {
                dag[scc[v]].push_back(scc[u]);
                ind[scc[u]]++;
            }
        }
}
void topsort()
{
    queue<int> q;
    for(int i=1;i<=id;i++)
     if(!ind[i])q.push(i);
     while(!q.empty())
     {
         int u=q.front();
         q.pop();
         if(!color[u])
          color[u]=1,color[f[u]]=2;
          for(int i=0;i<(int)dag[u].size();i++)
          {
              int v=dag[u][i];
              ind[v]--;
              if(!ind[v])q.push(v);
          }
     }
}
void solve(int n)
{
    for(int i=0;i<2*n;i++)
     if(!dfn[i])tarjan(i);
     for(int i=0;i<2*n;i+=2)
     if(scc[i]==scc[i+1])
     {
         printf("NIE\n");
         return;
     }
    else f[scc[i]]=scc[i+1],f[scc[i+1]]=scc[i];
    for(int i=0;i<=id;i++)
    dag[i].clear();
    buildDag(n);
    topsort();
    for(int i=0;i<2*n;i+=2)
    {
        if(color[scc[i]]==1)
        printf("%d\n",i+1);
        else
        printf("%d\n",i+2);
    }
}
int main()
{
//     freopen("in.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0,a,b;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            a--,b--;
            addEdage(a,b^1);
            addEdage(b,a^1);
        }
        solve(n);
    }
    return 0;
}
hdu 1814

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn=16005;
const int maxm=200005;
int head[maxn],next[maxm],to[maxm];
int color[maxn],ans[maxn];
int tot,top,cnt,id;
vector<int> dag[maxn];
void addEdage(int u,int v)
{
    next[tot]=head[u],to[tot]=v,head[u]=tot++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(color,0,sizeof(color));
    tot=top=cnt=id=0;
}
bool dfs(int v)
{
    if(color[v]==2)
     return false;
    if(color[v]==1)
     return true;
     ans[cnt++]=v;
    color[v]=1;
    color[v^1]=2;
    for(int i=head[v];i!=-1;i=next[i])
     if(!dfs(to[i]))
      return false;
    return true;
}
void solve(int n)
{
    for(int i=0;i<2*n;i++)
    {
        if(color[i])
        continue;
        cnt=0;
        if(!dfs(i))
        {

           for(int j=0;j<cnt;j++)
             {
                 color[ans[j]^1]=0;
                 color[ans[j]]=0;
             }
            if(!dfs(i^1))
            {
                printf("NIE\n");
                return;
            }
        }

    }
    for(int i=0;i<2*n;i+=2)
      if(color[i]==1)
         printf("%d\n",i+1);
      else
         printf("%d\n",i+2);

}
int main()
{
   //  freopen("in.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0,a,b;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            a--,b--;
            addEdage(a,b^1);
            addEdage(b,a^1);
        }
        solve(n);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值