HDU

来源:互联网 发布:天猫好还是淘宝好 编辑:程序博客网 时间:2024/06/05 22:51

HDU - 1814 

题意:直白的2-sat,但是这个是要输出最小的字典序,那么我们只能选择暴力。

思路:我的暴力的想法是,枚举每对元素,先优先取靠前的那个,把它所有连了边的都取了然后打tag,如果它不会产生矛盾,那么继续,如果有,那么先把所有的tag,再判断a+1有没有矛盾,如果a+1也有矛盾,那这组数据就已经gg了。我已经是个蠢到取消tag也要用dfs的人了,看了别人的暴力...基本都是用栈保留然后取消tag,我试了一下,我的两波dfs要跑2s.....而用栈处理.只需要1s不到。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 8e3 + 50, maxe = 2e5;struct node{    int to,next;    node(int a,int b){to = a; next = b;}    node(){}}edge[maxe];int h[maxn<<1];int n,m,edgenum,snum=0;int vis[maxn],col[maxn],ans[maxn],S[maxn];void add(int f,int t){    edge[edgenum] = node(t,h[f]);    h[f] = edgenum++;}int flag = 0;bool dfs(int u,int root){    if(col[u^1] != -1) return 0;    if(col[u] != -1) return 1;    col[u] = root;    for(int i = h[u]; ~i; i = edge[i].next)    {        int v = edge[i].to;        if(dfs(v,root) == 0) return 0;    }    return true;}void clean(int u){    col[u] = -1;    for(int i = h[u]; ~i; i = edge[i].next)    {        int v = edge[i].to;        if(col[v] != -1)            clean(v);    }}int main(){    int a,b;    while(~scanf("%d%d",&n,&m))    {        edgenum = snum = 0;        for(int i = 0; i < n*2 ; i++) h[i] = -1,col[i] = -1;        for(int i = 0; i < m ; i++)        {            scanf("%d%d",&a,&b); a--,b--;            add(a,b^1); add(b,a^1);        }        int mark = 0;        int num = 0;        for(int i = 0; i < n*2 ; i += 2)        {            if(col[i] == -1 && col[i+1] == -1)            {                if(dfs(i,i)) continue;                else                {                    clean(i);                    if(dfs(i+1,i+1))    continue;                    else {mark = 1;break;}                }            }        }        if(mark) {printf("NIE\n");}        else        {            for(int i = 0; i < n*2; i++)                if(col[i]!=-1)printf("%d\n",i+1);        }    }    return 0;}


原创粉丝点击