Pagini recente » Cod sursa (job #354342) | Cod sursa (job #529669) | Cod sursa (job #401160) | Cod sursa (job #2508252) | Cod sursa (job #2332773)
/*
_ _ ___ ___ _ __ __
__| | __ _ / | / _ \ / _ \ (_)/ / _ \ \
/ _` | / _` | | | | | | | | | | | / / (_) | |
| (_| | | (_| | | | | |_| | | |_| | / /_ _ | |
\__,_| \__,_| |_| \___/ \___/ /_/(_) (_) | |
/_/
*/
//#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <tuple>
#include <fstream>
#include <iomanip>
#include <utility>
#include <list>
#pragma warning "da 100% din tine. :)"
#define nl '\n'
#define cnl cout << '\n';
#define pb(x) push_back(x)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ll long long
#define ull unsigned ll
#ifdef INFOARENA
#define ProblemName "party"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "a.in"
#define OuFile "a.out"
#endif
using namespace std;
ifstream cin(InFile);
ofstream cout(OuFile);
template<class v, class type>
void print(v Vector, type nr) {
for_each(all(Vector), [](type x) {
cout << x << ' ';
});
}
struct vertex {
list<int> adi;
list<int> invadi;
};
int n;
vertex nod[300];
bitset<300> viz;
vector<vector<int> > ctc;
stack<int> finish;
int nrcomp = 0;
int sol[300];
int non(int nod) {
if (nod <= n)
return nod + n;
else return nod - n;
}
void edge(const int p1, const int p2) {
nod[p1].adi.push_back(p2);
nod[p2].invadi.push_back(p1);
}
void dfs(const int start) {
viz[start] = true;
for (auto it = nod[start].adi.begin(); it != nod[start].adi.end(); ++it) {
if (!viz[*it])
dfs(*it);
}
finish.push(start);
}
void dfsinv(const int start) {
viz[start] = true;
ctc[nrcomp].emplace_back(start);
for (auto it = nod[start].invadi.begin(); it != nod[start].invadi.end(); ++it) {
if (!viz[*it])
dfsinv(*it);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
clock_t tStart = clock();
int m, x, y, cod;
cin >> n >> m;
while (m--) {
cin >> x >> y >> cod;
switch (cod) {
case 0: {
edge(non(x), y);
edge(non(y), x);
break;
}
case 1: {
edge(non(x), non(y));
edge(y, x);
break;
}
case 2: {
edge(non(y), non(x));
edge(x, y);
break;
}
case 3: {
edge(x, non(y));
edge(y, non(x));
break;
}
}
}
for (int i = 1; i <= n; ++i) {
if (!viz[i])
dfs(i);
}
viz.reset();
vector<int> temp;
ctc.push_back(temp);
while (!finish.empty()) {
if (!viz[finish.top()]) {
ctc.push_back(temp);
++nrcomp;
dfsinv(finish.top());
}
finish.pop();
}
for (int i = 0; i <= 300; ++i)
sol[i] = -1;
int participanti = 0;
for (int i = nrcomp; i >= 1; --i) {
if (sol[ctc[i][0]] == -1) {
for (auto it = ctc[i].begin(); it != ctc[i].end(); ++it) {
sol[*it] = 1;
sol[non(*it)] = 0;
}
}
}
for (int i = 1; i <= n; ++i) {
if (sol[i] == -1) {
sol[i] = 1; //daca e -1 inseamna ca el nu are nicio muchie deci inseamna ca vine
}
participanti += sol[i];
}
cout << participanti << nl;
for (int i = 1; i <= n; ++i) {
if (sol[i])
cout << i << nl;
}
printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}