Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #include <bits/stdc++.h>
- using namespace std;
- const int OO = 1e9;
- const double EPS = 1e-9;
- map<int,bool> vis;
- set<int> nodes;
- map<int,vector<int>> graph;
- map<int,int> indeg;
- bool dfs(int x) {
- //cout << "here at x = " << x << "\n";
- if(vis[x]) {
- return false;
- }
- vis[x] = 1;
- bool ret = true;
- for(int e : graph[x]) {
- ret = dfs(e) && ret;
- }
- return ret;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int a,b;
- int ti = 0;
- while(true) {
- cin >> a >> b;
- if(a < 0 && b < 0) {
- break;
- }
- ti++;
- vis.clear();
- nodes.clear();
- graph.clear();
- indeg.clear();
- while(true) {
- if(a == 0 && b == 0) {
- break;
- }
- graph[a].push_back(b);
- indeg[b]++;
- nodes.insert(a);
- nodes.insert(b);
- cin >> a >> b;
- }
- int root = -1;
- bool tree = true;
- if(nodes.empty()) {
- goto done;
- }
- for(int x : nodes) {
- if(indeg[x] == 0) {
- if(root != -1) {
- tree = false;
- goto done;
- }
- root = x;
- }
- }
- if(root == -1) {
- tree = false;
- goto done;
- }
- tree = tree && dfs(root);
- for(int x : nodes) {
- if(!vis[x]) {
- tree = false;
- break;
- }
- }
- done:
- cout << "Case " << ti << " is " << (tree ? "a tree.\n":"not a tree.\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement