HDU1814PeacefulCommission (2

Source: Internet
Author: User
HDU1814PeacefulCommission question: according to the Constitution, the Public Peace Committee of the Byteland Democratic Republic should be created through legislation in Congress. Unfortunately, the discord between some party representatives makes it difficult. This committee must meet the following conditions: Each Party has exactly one generation in the Committee.

HDU 1814 Peaceful Commission Question: according to the Constitution, the Public Peace Committee of the Byteland Democratic Republic should be created through legislation in Congress. Unfortunately, the discord between some party representatives makes it difficult. This committee must meet the following conditions: Each Party has exactly one generation in the Committee.

HDU 1814 Peaceful Commission

Question Link

Question:
According to the Constitution, the Public Peace Committee of the Byteland Democratic Republic should be created through legislation in Congress. Unfortunately, the discord between some party representatives makes it difficult.

This committee must meet the following conditions:

Each party has exactly one representative in the Committee,
If two representatives hate each other, they cannot all belong to the Committee.
Each party has two representatives in the Parliament. Number from 1 to 2n. 2i-1 and 2i representatives belong to the I-th party.

Task

Write a program:

The number of Parties read from text files and the relationship is unfriendly,
Determines whether the establishment of the Peace Committee is possible. If so, the Committee's member table will be listed,
The result is written to a text file.
Input

The first line of the text file contains 2 non-negative integers n and m. They respectively said: the number of Parties n, 1 <= n <= 8000 and unfriendly representatives of m, 0 <= m <= 20000. Each behavior in the following m rows is an integer a, B, 1 <=

Output

If the Committee cannot be created, the text file should contain the word "NIE. If it can be true, the text file SPO. OUT should include n integers selected from the range 1 to 2n, written in ascending order, each row one, these numbers represent in the Committee. If the Committee can be formed in multiple ways, the program can write only one of them.

Idea: Obviously, the 2-sat problem is that the smallest Lexicographic Order is output, so we use each person as the node, 0, 1 indicates selection or not selection. Each time we start from the smallest person, we try to select 1, the final output path is enough.

Code:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include using namespace std;const int MAXNODE = 16005;struct TwoSet {int n;vector
     
       g[MAXNODE * 2];bool mark[MAXNODE * 2];int S[MAXNODE * 2], sn;void init(int tot) {n = tot * 2;for (int i = 0; i < n; i += 2) {g[i].clear();g[i^1].clear();}memset(mark, false, sizeof(mark));}void add_Edge(int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].push_back(v);g[v^1].push_back(u);}void delete_Edge(int u, int uval, int v, int vval) {u = u * 2 + uval;v = v * 2 + vval;g[u^1].pop_back();g[v^1].pop_back();}bool dfs(int u) {if (mark[u^1]) return false;if (mark[u]) return true;mark[u] = true;S[sn++] = u;for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (!dfs(v)) return false;}return true;}bool solve() {for (int i = 0; i < n; i += 2) {if (!mark[i] && !mark[i + 1]) {sn = 0;if (!dfs(i + 1)){for (int j = 0; j < sn; j++)mark[S[j]] = false;sn = 0;if (!dfs(i)) return false;}}}return true;}} gao;int n, m;int main() {while (~scanf("%d%d", &n, &m)) {gao.init(n * 2);for (int i = 0; i < n * 2; i += 2) {gao.add_Edge(i, 0, i + 1, 0);gao.add_Edge(i, 1, i + 1, 1);}int u, v;while (m--) {scanf("%d%d", &u, &v);u--; v--;gao.add_Edge(u, 0, v, 0);}if (!gao.solve()) printf("NIE\n");else {for (int i = 0; i < n * 2; i++)if (gao.mark[i * 2 + 1]) printf("%d\n", i + 1);}}return 0;}
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.