I am getting Wrong Answer, so i want to make sure of some issues:
- Is it only one input set ??
- My splution is as follows:
I buid a tree rooted by the first person that appears in the input and traverse this tree, once right to left and once left to right. If the 2 traversals give the same result then we have only 1 listing, otherwise we have 2 listings as the solution, is that right ??
-Any other special cases ??
11131 - Close Relatives
Moderator: Board moderators
Well i cannot find why this does not work.
Code: Select all
FILE *in = stdin;
vector<string> tokenize(string s, string ch) {
vector<string> ret;
for( int i = 0, j; i < s.size(); i = j+1 ) {
j = s.find_first_of(ch, i);
if( j == -1 ) j = s.size();
if( j-i > 0 ) ret.push_back( s.substr(i, j-i) );
}
return ret;
}
string readline()
{
char ch;
string ret;
while(fscanf(in,"%c",&ch)!=EOF)
{
if(ch=='\n') return ret;
ret+=ch;
}
return ret;
}
class node { public:
vi a;
string name;
};
map<string,int> p;
node z[6000];
void solve(int turn)
{
int i;
fo(i,z[turn].a.size())
solve(z[turn].a[i]);
printf("%s\n",z[turn].name.c_str());
}
void solve2(int turn)
{
int i;
for(i=z[turn].a.size()-1 ; i>=0 ; i--)
solve(z[turn].a[i]);
printf("%s\n",z[turn].name.c_str());
}
int main()
{
int i,n,k=0,flag=0;
string s;
vs v;
char ch;
fo(i,6000)
z[i].a.clear();
while(1)
{
s=readline();
if(s.size()==0) break;
v=tokenize(s,",");
fo(i,v.size())
if(p.find(v[i])==p.end())
{
z[k].name=v[i];
p[v[i]]=k++;
}
for(i=1 ; i<v.size() ; i++)
z[p[v[0]]].a.push_back(p[v[i]]);
}
fo(i,6000)
if(z[i].a.size()>1)
flag=1;
if(flag)
{
printf("2\n\n");
solve(0);
printf("\n");
solve2(0);
}
else
{
printf("1\n\n");
solve(0);
}
return 0;
}
-
- New poster
- Posts: 44
- Joined: Sun Apr 27, 2003 3:17 am
- Location: Rio Grande do Norte - Brazil
- Contact:
-
- New poster
- Posts: 44
- Joined: Sun Apr 27, 2003 3:17 am
- Location: Rio Grande do Norte - Brazil
- Contact:
Yes. Note that any set of lists you give will either uniquely define a tree or be invalid because it defines a general DAG rather than a tree -- i.e. it defines someone to have several children (which I guess you could view as an ambiguity).danielrocha wrote:I'm sorry. I've re-re-read the problem statement, and it's getting clearer. So I'm suppose to give the minimum amount of lists that would allow the unambiguous reconstruction of the tree?[/list]
Re: 11131 Close Relatives
Mine is always print 2 ways of listing using post order traversal and got AC.taha wrote: I buid a tree rooted by the first person that appears in the input and traverse this tree, once right to left and once left to right. If the 2 traversals give the same result then we have only 1 listing, otherwise we have 2 listings as the solution, is that right ??
-Any other special cases ??
The problem statement didn't mention what is 'm' means! Are we supposed to be guess it? So my experiments above shows that the judge output is always 2 (or is it because of the single data only)?
IMHO, the problem is not very clear on 'm'.
Visit my script to hunt UVA problem here:
http://felix-halim.net/uva/hunting/
-----------------------
Felix Halim
http://felix-halim.net/uva/hunting/
-----------------------
Felix Halim