Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- L3-2-SAT
- Agata Latacz
- */
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <stack>
- /*rgaf dwudzielny, ludzie z partii: xi oraz ~xi
- 1. Budujemy graf.
- 2. Silnie spójnie składowe
- 3.2. szukamy Silnie spójnych składowych, za pomocą DFS-a z zapamiętaniem wierzchołków na stosie,
- a następnie DFS w kolejności spamiętanych wierzchołków na grafie odwróconym
- 4.
- nieparzysty xi
- parzysty ~xi
- */
- using namespace std;
- vector<int> V[200001];
- vector<int> A[200001];
- vector<int> SK[200001];
- vector<int> Wynik;
- int COL[200001];
- int SCC[200001];
- int W[200001];
- bool BOOL[200001];
- stack<int>Stos;
- stack<int>GONE;
- int DFS(int x)
- {
- //printf("DFS, x: %d\n",x);
- COL[x]=1;
- for(int i=0;i<V[x].size(); i++){
- int b=V[x][i];
- if(COL[b]==0)DFS(b);
- }
- Stos.push(x);
- GONE.push(x);
- }
- bool DFS2(int x, int col)
- {
- SK[col].push_back(x);
- //printf("DFS2, x: %d, col: %d\n", x, col);
- SCC[x]=col;
- if(SCC[(x+1)/2*2-(x+1)%2]==SCC[x])return false;
- bool CZY=1;
- for(int i=0;i<A[x].size();i++){
- int b=A[x][i];
- if(SCC[b]==0){if(DFS2(b,col)==0) CZY=0;}
- }
- return CZY;
- }
- void WYK(int a){
- }
- int main()
- {
- int n,m;
- scanf("%d %d",&n,&m);
- for(int i=0;i<m;i++){
- int x,y;
- scanf("%d %d",&x,&y);
- int nx=(x+1)/2*2-(x+1)%2;
- int ny=(y+1)/2*2-(y+1)%2;
- V[x].push_back(ny);
- V[y].push_back(nx);
- A[nx].push_back(y);
- A[ny].push_back(x);
- }
- for(int i=1;i<=2*n;i++)
- if(COL[i]==0)DFS(i);
- int s=1;
- bool O=1;
- while(!Stos.empty()){
- W[s]=-1;
- int a=Stos.top();
- Stos.pop();
- if(DFS2(a,s)==0){printf("NIE\n");O=0;break;}
- s++;
- }
- s--;
- if(O)
- {
- while(!GONE.empty())
- {
- int a=GONE.top();
- GONE.pop();
- if(W[SCC[a]]==0)Wynik.push_back(a);
- if(BOOL[SCC[a]]==0){
- if(W[SCC[a]]!=-1)
- for(int i=0;i<SK[SCC[a]].size();i++)
- W[SCC[(a+1)/2*2-(a+1)%2]]=(W[SCC[a]]+1)%2;
- else
- {
- W[SCC[a]]=1;
- for(int i=0;i<SK[SCC[a]].size();i++)
- W[SCC[(a+1)/2*2-(a+1)%2]]=(W[SCC[a]]+1)%2;
- }
- }
- }
- sort(Wynik.begin(), Wynik.end());
- for(int i=0;i<Wynik.size();i++)
- printf("%d\n",Wynik[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement