Cod sursa(job #1302630)

Utilizator wGEORGEWGeorge Cioti wGEORGEW Data 27 decembrie 2014 08:23:49
Problema Party Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.1 kb
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
using namespace std;
 
const int NMAX = 105;
 
vector <int> graph[NMAX * 2], trans[NMAX * 2];
int n, val[NMAX * 2], vis[NMAX * 2];
stack <int> st;
 
inline int neg (int x) {
    return x > n ? x - n : x + n;
}
 
void dfsPlus (int node) {
    vis[node] = 1;
    for(vector <int>::iterator it = graph[node].begin(); it != graph[node].end(); ++ it)
        if(!vis[*it])
            dfsPlus(*it);
    st.push(node);
}
 
void dfsMinus (int node) {
    vis[node] = 1;
    val[neg(node)] = 1;
    //fprintf(stderr, " %d", node);
    for(vector <int>::iterator it = trans[node].begin(); it != trans[node].end(); ++ it)
        if(!vis[*it])
            dfsMinus(*it);
}
 
int main() {
    freopen("party.in", "r", stdin);
    freopen("party.out", "w", stdout);
    int m, i, j, x, y, c;
 
    scanf("%d%d", &n, &m);
    for(i = 1; i <= m; ++ i) {
        scanf("%d%d%d", &x, &y, &c);
        if(c == 0) {
            graph[neg(x)].push_back(y);
            trans[y].push_back(neg(x));
 
            graph[neg(y)].push_back(x);
            trans[x].push_back(neg(y));
        }
 
        if(c == 1) {
            graph[neg(x)].push_back(neg(y));
            trans[neg(y)].push_back(neg(x));
        }
 
        if(c == 2) {
            graph[neg(y)].push_back(neg(x));
            trans[neg(x)].push_back(neg(y));
        }
 
        if(c == 3) {
            graph[x].push_back(neg(y));
            graph[neg(y)].push_back(x);
 
            graph[y].push_back(neg(x));
            graph[neg(x)].push_back(y);
        }
    }
 
    for(i = 1; i <= 2 * n; ++ i)
        if(!vis[i])
            dfsPlus(i);
 
    memset(vis, 0, sizeof(vis));
    while(!st.empty()) {
        if(!vis[st.top()] && !vis[neg(st.top())]) {
      //      fprintf(stderr, "\n");
            dfsMinus(st.top());
        }
        st.pop();
    }
 
    c = 0;
    for(i = 1; i <= n; ++ i)
        c += val[i];
 
    printf("%d\n", c);
    for(i = 1; i <= n; ++ i)
        if(val[i])
            printf("%d\n", i);
    return 0;
}