Skip to content

Instantly share code, notes, and snippets.

@sergiobuj
Created September 16, 2014 01:47
Show Gist options
  • Select an option

  • Save sergiobuj/eb5c2a3a4fe8d94afd4b to your computer and use it in GitHub Desktop.

Select an option

Save sergiobuj/eb5c2a3a4fe8d94afd4b to your computer and use it in GitHub Desktop.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
map<int, vector<int> > graph;
bool count_down(int start){
queue<int> tovisit;
set<int> visited;
tovisit.push(start);
while(tovisit.size()){
int node = tovisit.front();
visited.insert(node);
tovisit.pop();
for(int i=0; i<graph[node].size(); ++i){
int sib = graph[node][i];
if(!visited.count(sib)){
tovisit.push(sib);
}
}
}
return visited.size()%2 == 0 && visited.size() >= 2;
}
int main() {
int M,N;
cin >> M >> N;
for(int i=0; i<N; ++i){
int u, v;
cin >> u >> v;
graph[v].push_back(u);
}
int ans = 0;
for(int i=1; i<=M; ++i){
ans += count_down(i) ? 1 : 0;
}
printf("%d\n", ans - 1);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment