Peaceful Commission HDU - 1814(2-SAT)

The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.

The Commission has to fulfill the following conditions:
1.Each party has exactly one representative in the Commission,
2.If two deputies do not like each other, they cannot both belong to the Commission.

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .

Task
Write a program, which:
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members,
3.writes the result in the text file SPO.OUT.
Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.
There are multiple test cases. Process to end of file.
Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence.
Sample Input
3 2
1 3
2 4
Sample Output
1
4
5
这题我给做复杂了,没看到要求字典序,所以用了(2-SAT)的通用解法,强连通分量,缩点啥的,但是他要字典需,那句只能暴力了,暴力写起来还简单点

#include
#include
#include
#include
#include
#include
#include
#include
#define N 16005
#define mod 530600414
using namespace std;
int n,m;
vector<int> graph[N];
int c[N];
int temp[N];
int cnt;
bool dfs(int x)
{
    if(c[x]==1)return true;
    if(c[x]==-1)return false;
    c[x]=1;
    if(x&1)c[x+1]=-1;
    else
        c[x-1]=-1;
    temp[cnt++]=x;
    for(int i=0;iif(!dfs(graph[x][i]))
            return false;
    return true;
}
int main()
{
    int x,y;
    while(scanf("%d%d",&n,&m)==2)
    {

        for(int i=1;i<=n*2;i++)
            graph[i].clear();

        while(m--)
        {
            scanf("%d%d",&x,&y);
            if(y&1)
                graph[x].push_back(y+1);
            else
                graph[x].push_back(y-1);
            if(x&1)
                graph[y].push_back(x+1);
            else
                graph[y].push_back(x-1);

        }
        memset(c,0,sizeof(c));
        bool sign=true;
        for(int i=1;i<=n;i++)
        {
            int now=(i<<1)-1;
            if(c[now])
                continue;
            cnt=0;
            if(!dfs(now))
            {
                for(int j=0;j0;
                    if(temp[j]&1)
                        c[temp[j]+1]=0;
                    else
                        c[temp[j]-1]=0;
                }
                if(!dfs(now+1))
                {
                    sign=false;
                    break;
                }
            }
        }
        if(sign)
        {

            for(int i=1;i<=n*2;i++)
            {

                if(c[i]==1)
                    printf("%d\n",i);
            }
        }
        else
            printf("NIE\n");
    }
    return 0;
}

这个非字典需解法,效率会高,但是会wa

#include
#include
#include
#include
#include
#include
#include
#include
#define N 16005
#define mod 530600414
using namespace std;
int n,m;
vector<int> graph[N];
int belong[N];//点缩点以后的点的index和当前index之间的所属关系
int index1;
int DFN[N],low[N];
bool inS[N];
int countt;
stack<int>st;
void tarjan(int x)
{
    DFN[x]=low[x]=index1++;
    st.push(x);
    inS[x]=true;
    for(int i=0;iint v=graph[x][i];
        if(!DFN[v])
        {
            tarjan(v);
            low[x]=min(low[v],low[x]);
        }
        else
            if(inS[v])
            low[x]=min(low[x],DFN[v]);
    }
    if(DFN[x]==low[x])
    {
        countt++;
        while(st.top()!=x)
        {
            belong[st.top()]=countt;
            inS[st.top()]=false;
            st.pop();
        }
        belong[st.top()]=countt;
        inS[st.top()]=false;
        st.pop();
    }
}
int opp[N];//对立点
int inD[N];
short vis[N];
vector<int>g[N];
void work()//后面主要就是拓扑排序
{
    for(int i=1;i<=countt;i++)
        g[i].clear();
    memset(inD,0,sizeof(inD));
    memset(vis,0,sizeof(vis));
    for(int u=1;u<=n*2;u++)
    {
        for(int i=0;iint v=graph[u][i];
            if(belong[v]!=belong[u])
            {
                inD[u]++;
                g[v].push_back(u);
            }
        }
    }//缩点以后建的图
    memset(vis,0,sizeof(vis));
    queue<int> que;
    for(int i=1;i<=countt;i++)
        if(!inD[i])que.push(i);
    while(!que.empty())//拓扑排序
    {
        int u=que.front();
        que.pop();
        if(!vis[u])
        {
            vis[u]=1;
            vis[opp[u]]=-1;
        }
        for(int i=0;iint v=g[u][i];
            inD[v]--;
            if(!inD[v])
                que.push(v);
        }
    }
    for(int i=1;i<=2*n;i++)
        if(vis[belong[i]]==1)
        printf("%d\n",i);
}
int main()
{
    int x,y;
    while(scanf("%d%d",&n,&m)==2)
    {
        index1=1;
        countt=0;
        for(int i=1;i<=n*2;i++)
            graph[i].clear();
        memset(DFN,0,sizeof(DFN));
        while(m--)
        {
            scanf("%d%d",&x,&y);
            if(y&1)
                graph[x].push_back(y+1);
            else
                graph[x].push_back(y-1);
            if(x&1)
                graph[y].push_back(x+1);
            else
             graph[y].push_back(x-1);

        }
        for(int i=1;i<=2*n;i++)
            if(!DFN[i])
                tarjan(i);
        /*for(int i=1;i<=2*n;i++)
            cout<
        bool sign=false;
        for(int i=1;i<=n;i++)
            if(belong[2*i-1]==belong[2*n])
            {
                sign=true;
                break;
            }
            else
            {
                opp[belong[2*i-1]]=belong[2*i];
                opp[belong[2*i]]=belong[2*i-1];
            }
        if(sign)
            printf("NIE\n");
        else
            work();
    }
    return 0;
}

你可能感兴趣的:(tarjan,拓扑排序,2-SAT)