UVa 615 - Is It A Tree?
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 500;
vii adj[high];
int indegree[high], visited[high] , par[high];
void clr()
{
for(int i=0; i<high; i++)
{
par[i] = 0;
visited[i] = 0;
indegree[i] = 0;
adj[i].clear();
}
}
void notree()
{
cout << " is not a tree.\n";
}
void tree()
{
cout << " is a tree.\n";
}
bool checkIndegree(int cnt)
{
int i=0;
for(i=1; i<cnt; i++)
{
if(indegree[i] > 1) return false;
}
return true;
}
bool checkParent(int cnt)
{
int pars = 0;
for(int i=1; i<cnt; i++)
{
if(!par[i]) pars+=1;
}
if(pars > 1) return false;
return true;
}
void DFS(int u)
{
visited[u] = 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
int main()
{
fast;
mpii mp;
int i , x , y , a , b , Tc = 0;
while(cin >> a >> b)
{
if(a < 0 && b < 0) break;
mp.clear();
clr();
if(a == 0 && b == 0)
{
cout << "Case " << ++Tc;
tree();
continue;
}
int cnt = 1;
bool cycle = false;
if(!mp.count(a))
{
mp[a] = cnt++;
}
x = mp[a];
if(!mp.count(b))
{
mp[b] = cnt++;
}
y = mp[b];
adj[x].psb(y);
par[y] = x;
if(x == y) cycle = true;
indegree[y]++;
while(cin >> a >> b)
{
if(!a and !b) break;
if(!mp.count(a))
{
mp[a] = cnt++;
}
x = mp[a];
if(!mp.count(b))
{
mp[b] = cnt++;
}
y = mp[b];
adj[x].psb(y);
par[y] = x;
if(x == y) cycle = true;
indegree[y]++;
}
cout << "Case " << ++Tc;
if(!cycle)
{
bool indegreeOne = checkIndegree(cnt);
if(!indegreeOne) notree();
else
{
bool parentOne = checkParent(cnt);
if(!parentOne) notree();
else
{
int tmpar = 0;
for(i=1; i<cnt; i++)
{
if(!par[i])
{
tmpar = i;
break;
}
}
DFS(tmpar);
bool connected = true;
for(i=1; i<cnt; i++)
{
if(!visited[i])
{
connected = false;
break;
}
}
if(connected) tree();
else notree();
}
}
}
else
{
notree();
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 500;
vii adj[high];
int indegree[high], visited[high] , par[high];
void clr()
{
for(int i=0; i<high; i++)
{
par[i] = 0;
visited[i] = 0;
indegree[i] = 0;
adj[i].clear();
}
}
void notree()
{
cout << " is not a tree.\n";
}
void tree()
{
cout << " is a tree.\n";
}
bool checkIndegree(int cnt)
{
int i=0;
for(i=1; i<cnt; i++)
{
if(indegree[i] > 1) return false;
}
return true;
}
bool checkParent(int cnt)
{
int pars = 0;
for(int i=1; i<cnt; i++)
{
if(!par[i]) pars+=1;
}
if(pars > 1) return false;
return true;
}
void DFS(int u)
{
visited[u] = 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
int main()
{
fast;
mpii mp;
int i , x , y , a , b , Tc = 0;
while(cin >> a >> b)
{
if(a < 0 && b < 0) break;
mp.clear();
clr();
if(a == 0 && b == 0)
{
cout << "Case " << ++Tc;
tree();
continue;
}
int cnt = 1;
bool cycle = false;
if(!mp.count(a))
{
mp[a] = cnt++;
}
x = mp[a];
if(!mp.count(b))
{
mp[b] = cnt++;
}
y = mp[b];
adj[x].psb(y);
par[y] = x;
if(x == y) cycle = true;
indegree[y]++;
while(cin >> a >> b)
{
if(!a and !b) break;
if(!mp.count(a))
{
mp[a] = cnt++;
}
x = mp[a];
if(!mp.count(b))
{
mp[b] = cnt++;
}
y = mp[b];
adj[x].psb(y);
par[y] = x;
if(x == y) cycle = true;
indegree[y]++;
}
cout << "Case " << ++Tc;
if(!cycle)
{
bool indegreeOne = checkIndegree(cnt);
if(!indegreeOne) notree();
else
{
bool parentOne = checkParent(cnt);
if(!parentOne) notree();
else
{
int tmpar = 0;
for(i=1; i<cnt; i++)
{
if(!par[i])
{
tmpar = i;
break;
}
}
DFS(tmpar);
bool connected = true;
for(i=1; i<cnt; i++)
{
if(!visited[i])
{
connected = false;
break;
}
}
if(connected) tree();
else notree();
}
}
}
else
{
notree();
}
}
return 0;
}
Comments
Post a Comment