No, you are wrong. All the edge description must be in pair (a,b) which means there is an edge from vertex a --> b.input:
1 2
1
4 5
0 0
There is no end vertex for 1 in second line...

Moderator: Board moderators
No, you are wrong. All the edge description must be in pair (a,b) which means there is an edge from vertex a --> b.input:
1 2
1
4 5
0 0
Code: Select all
array[1..1000] of array[1..1000] of integer
Code: Select all
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
0 0
1 1 0 0
1 2 2 1 5 5 0 0
1 2
2 1
4 5
0 0
1 2
2 3
3 4
4 1
0 0
1 2 2 3 3 1 4 5 0 0
1 1 0 0
1 2
1 2
0 0
1 2
2 1
0 0
1 2
3 4
4 3
0 0
1 2 2 3 3 1 4 5 0 0
1 2
1 3
2 4
2 5
2 6
2 7
3 8
8 9
9 10
10 11
11 12
12 13
0 0
10006 10008 10005 10003 10005 10002 10006 10004
10005 10006 0 0
99006 700002 700002 880002 880002 1000001 1000001 1234567890
1234567890 22 0 0
-1 -1
Code: Select all
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
Case 4 is a tree.
Case 5 is not a tree.
Case 6 is not a tree.
Case 7 is not a tree.
Case 8 is not a tree.
Case 9 is not a tree.
Case 10 is not a tree.
Case 11 is not a tree.
Case 12 is not a tree.
Case 13 is not a tree.
Case 14 is not a tree.
Case 15 is a tree.
Case 16 is a tree.
Case 17 is a tree.
Code: Select all
6 8 5 3 5 2 6 4
5 6 0 0
hey man, I think that's a wrong case. Because 1 1 0 0 is a tree with one root. 1 2 1 2 0 0 is simply a tree with nodes 1 and 2, with 1 being the parent of 2.Sedefcho wrote: 1 1 0 0
1 2
1 2
0 0
...
Case 10 is not a tree.
Case 11 is not a tree.
[/code]
Code: Select all
0 0
Code: Select all
#include<iostream.h>
int a[1000][1000],cn=0,chk[1000],ml;
void treechk(int i){
chk[i] = 1;
int j;
for(j = 0; j<1000; j++){
if(a[i][j] > 0){
if(chk[j] == 1){
cout << "Case " << cn+1 << " is not a tree." << endl;
return;
ml=1;
}
treechk(j);
}
}return;
}
void main(){
int x,y,t=0,i,j,m;
while(cin >> x >> y){
if(x!=-1&&y!=-1){
if(x==0 && y==0){
if(t==0){
treechk(0);
for(i = 0; i<1000; i++){
if(chk[i]==1) m=chk[i];
}for(i = 0; i<m; i++){
if(chk[i] == 0){
cout << "Case " << cn+1 << " is not a tree." << endl;
ml=1;
break;
}
}
if(ml==0) cout << "Case " << cn+1 << " is a tree." << endl;
cn++;
}
for(i = 0; i<1000; i++){
for(j = 0; j<1000; j++){
a[i][j] = 0;
}
chk[i] = 0;
}
t=0;
ml=0;
m=0;
}else{
if(a[x-1][y-1] == 1 || a[y-1][x-1] == 1){
t=1;
cout << "Case " << cn+1 << " is not a tree." << endl;
}else{
a[x-1][y-1]=1;
a[y-1][x-1]=2;
}
}
}else break;
}
}
Code: Select all
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
Code: Select all
Case 1 is not a tree.
Case 1 is a tree.
Code: Select all
1 2 1 3 2 4 2 5 2 6 2 7 3 8 8 9 9 10 10 11 11 12 12 13 0 0
Code: Select all
Case 2 is not a tree.
Case 2 is not a tree.
Case 2 is a tree.
Code: Select all
1 2 1 2 0 0
1 2 2 1 0 0
1 2 3 4 4 3 0 0
Another thing I found out when I look into your program: the node number could be greater then 10000(refers from this post and this post), where your code can only handle up to 1000.The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers.
Code: Select all
#include<stdio.h>
#include<memory.h>
#define MAX 5400
bool a[MAX][MAX];
int b[MAX];
int findroot(int max)
{
int i,j;
int nor=0,root=0,sum;
for(i=1;i<=max;i++)
{
if(b[i])
{
sum=0;
for(j=1;j<=max;j++)
{
if(a[j][i])
{
sum++;
}
}
if(sum==0)
{
root=i;
nor++;
}
if(sum>1)
{
return -1;
}
if(nor>1)
{
return -1;
}
}
}
return root;
}
int DFS(int n,int max)
{
int i,j;
if(b[n]>1)
{
return -1;
}
b[n]++;
for(i=1;i<=max;i++)
{
if(a[n][i])
{
j=DFS(i,max);
if(j<0)
{
return j;
}
}
}
return 1;
}
int isVisited(int max)
{
int i;
for(i=1;i<=max;i++)
{
if(b[i]==1)
{
return -1;
}
}
return 1;
}
int main(void)
{
int m,n,max,t=0;
while(1)
{
scanf("%d%d",&m,&n);
if(m<0 || n<0)
{
break;
}
t++;
if(m==0 && n==0)
{
printf("Case %d is not a tree.\n",t);
continue;
}
max=0;
memset(a,false,sizeof(a));
memset(b,0,sizeof(b));
a[m][n]=true;
b[m]=1;
b[n]=1;
if(m>max)
{
max=m;
}
if(n>max)
{
max=n;
}
while(1)
{
scanf("%d%d",&m,&n);
if(m==0 && n==0)
{
break;
}
a[m][n]=true;
b[m]=1;
b[n]=1;
if(m>max)
{
max=m;
}
if(n>max)
{
max=n;
}
}
printf("Case %d is ",t);
int root=findroot(max);
if(root<=0)
{
printf("not a tree.\n");
continue;
}
int check=DFS(root,max);
if(check<0)
{
printf("not a tree.\n");
continue;
}
check=isVisited(max);
if(check<0)
{
printf("not a tree.\n");
continue;
}
printf("a tree.\n");
}
return 0;
}
Code: Select all
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#ifndef ONLINE_JUDGE
#define show(x) cout << #x << " = " << x << endl
#else
#define show(x)
#endif
#define FOR(i, n) for(int i = 0; i < n; i ++)
#define FORE(i, m, n) for(int i = m; i < n; i ++)
int indegree[1000000];
bool used[1000000];
int parent[1000000];
int main() {
#ifndef ONLINE_JUDGE
freopen("IsItATree.in", "r", stdin);
#endif
int x, y;
int tt = 0;
while(cin >> x >> y, x+y >= 0) {
tt ++;
if(x+y == 0) {
cout << "Case " << tt << " is a tree." << endl;
continue;
}
memset(indegree, 0, 1000000*sizeof(int));
memset(used, 0, 1000000*sizeof(bool));
memset(parent, 0, 1000000*sizeof(int));
int v = (x == y ? 1 : 2), e = 1;
int grandparent = x;
parent[y] = x;
indegree[y] ++;
used[x] = used[y] = true;
while(cin >> x >> y, x+y) {
//cout << x << " " << y << endl;
e ++;
if(!used[x])
v ++;
if(!used[y])
v ++;
int p = x;
while(parent[p])
p = parent[p];
//show(p);
int n = x;
while(parent[n]) {
n = parent[n];
if(n != p)
parent[n] = p;
}
if(y != p)
parent[y] = p;
indegree[y] ++;
used[x] = used[y] = true;
}
//show("gholi");
bool ok = true;
int zero = 0;
FOR(i, 1000000) {
if(!used[i])
continue;
if(indegree[i] == 0)
zero ++;
else if(indegree[i] > 1) {
ok = false;
break;
}
}
/*FOR(i, 1000000)
if(used[i])
cout << i << " " << parent[i] << endl;
*/
bool parentfail = false;
FOR(i, 1000000)
if(used[i] && parent[i] != grandparent) {
parentfail = true;
break;
}
//show(zero);
if(v == e + 1 && ok && zero == 1 && !parentfail)
cout << "Case " << tt << " is a tree." << endl;
else
cout << "Case " << tt << " is not a tree." << endl;
}
return 0;
}