1229 - Sub-dictionary (Why WA)

All about problems in Volume XII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

1229 - Sub-dictionary (Why WA)

Postby @li_kuet » Fri May 25, 2012 6:33 pm

first i did top sort using dfs
then find the scc
then again did dfs in the top list order and calculate them .

i dont know what is wrong :(
is it the wrong algorithm or system i applied ???

please help :(
Last edited by @li_kuet on Tue Jun 05, 2012 12:25 pm, edited 1 time in total.
@li_kuet
New poster
 
Posts: 24
Joined: Fri May 25, 2012 6:22 pm
Location: Chittagong, Bangladesh

Re: 1229 - Sub-dictionary (Why WA)

Postby brianfry713 » Fri Jun 01, 2012 1:07 am

Doesn't match the sample I/O.
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1229 - Sub-dictionary (Why WA)

Postby @li_kuet » Fri Jun 01, 2012 7:43 pm

How can i solve this problem ??? @brianfry713
@li_kuet
New poster
 
Posts: 24
Joined: Fri May 25, 2012 6:22 pm
Location: Chittagong, Bangladesh

Re: 1229 - Sub-dictionary (Why WA)

Postby brianfry713 » Fri Jun 01, 2012 11:48 pm

DFS
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1229 - Sub-dictionary (Why WA)

Postby plamplam » Fri Jul 27, 2012 10:50 pm

I'm getting wrong answer, what is the output for the test case? The problem is ambigous...it doesn't clearly state what should be the output when multiple solutions exists.

Code: Select all
9
d e
e f
f d
a b
b c
c a
g h
h i
i g
0
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
User avatar
plamplam
Experienced poster
 
Posts: 151
Joined: Fri May 06, 2011 11:37 am

Re: 1229 - Sub-dictionary (Why WA)

Postby brianfry713 » Fri Jul 27, 2012 11:31 pm

AC output for your input:
Code: Select all
9
a b c d e f g h i
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1229 - Sub-dictionary (Why WA)

Postby Mukit Chowdhury » Wed Nov 07, 2012 4:41 pm

what should be the output for input...
5
a s d f
b d f r
c q w e
f s
g h
0

My code gives...
3
d f s

It's wrong...isn't it ??
I am trying using topological sort ... Can't it be done using topological sort ???
Mukit Chowdhury
Learning poster
 
Posts: 59
Joined: Fri Aug 17, 2012 9:23 pm
Location: CUET

Re: 1229 - Sub-dictionary (Why WA)

Postby brianfry713 » Wed Nov 07, 2012 10:38 pm

Your input isn't valid.
A dictionary must contain the definition for any word that appears in the explanation of another word.
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1229 - Sub-dictionary (Why WA)

Postby Mukit Chowdhury » Thu Nov 08, 2012 6:47 pm

Checking... :(
Last edited by Mukit Chowdhury on Sun Feb 03, 2013 3:27 pm, edited 1 time in total.
Mukit Chowdhury
Learning poster
 
Posts: 59
Joined: Fri Aug 17, 2012 9:23 pm
Location: CUET

Re: 1229 - Sub-dictionary (Why WA)

Postby Ronok1307 » Fri Feb 01, 2013 10:20 pm

WA.
My algo -
1.use tarjan to find the scc dag
2. build dag graph
3. visit the nodes in the dag in topological order and visit the connected nodes
4. all the nodes in the dag, that act as a starting point for the dfs() call are the answers.
5. list all the nodes of the answers, sort them and show output

is the algo wrong?

Code: Select all
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;


struct node
{
   int id,low;
   bool operator <(const node &a)const
   {
      return a.low>low;
   }
}asd;

priority_queue <node> pq;
char ss[100];
string s,p,arr[105];
map <string,int> mp;
bool v[105],vv[105],vvv[105];
int adj[105][105],pre[105],post[105],ma,ll,l,dagl,whichdag[105],dagpre[105],dagpost[105],dagadj[105][105],lll;
vector <int> dag[105];
vector <string> ans;
stack <int> x;

void clear()
{
   while(!pq.empty())
      pq.pop();
   mp.clear();
   memset(v,0,sizeof(v));
   memset(vv,0,sizeof(vv));
   memset(vvv,0,sizeof(vvv));
   memset(adj,0,sizeof(adj));
   memset(dagadj,0,sizeof(adj));
   for(int i=0;i<dagl;i++)
      dag[i].clear();
   ans.clear();
   while(!x.empty())
      x.pop();
}

void rec(int n)
{
   pre[n]=post[n]=ll++;
   v[n]=vv[n]=1;
   x.push(n);
   int a,i;
   for(i=0;i<l;i++)
   {
      if(adj[i][n])
      {
         if(!v[i])
            rec(i);
         if(vv[i])
            post[n]=post[n]<post[i]?post[n]:post[i];
      }
   }
   if(pre[n]==post[n])
   {
      while(1)
      {
         a=x.top();
         x.pop();
         vv[a]=0;
         dag[dagl].push_back(a);
         whichdag[a]=dagl;
         if(a==n)
            break;
      }
      dagl++;
   }
}

void dfs(int n,int s)
{
   dagpre[n]=lll++;
   v[n]=1;
   int i;
   for(i=0;i<dagl;i++)
   {
      if(dagadj[n][i] && !v[i])
         dfs(i,s);
   }
   dagpost[n]=lll++;
   if(s)
   {
      asd.id=n;
      asd.low=dagpost[n];
      pq.push(asd);
   }
}

int main()
{
   //freopen("in.txt","r",stdin);
   int a,b,c,i,j,k,n,t;
   while(1)
   {
      sscanf(gets(ss),"%d",&n);
      if(!n)
         break;
      l=0;
      while(n--)
      {
         getline(cin,s);
         stringstream line(s);
         line>>p;
         if(mp.find(p)==mp.end())
         {
            mp[p]=l;
            b=l;
            arr[l]=p;
            l++;
         }
         else
            b=mp[p];
         while(line>>p)
         {   
            if(mp.find(p)==mp.end())
            {
               mp[p]=l;
               a=l;
               arr[l]=p;
               l++;
            }
            else
               a=mp[p];
            adj[a][b]=1;
         }
      }
      dagl=ll=0;
      for(i=0;i<l;i++)
      {
         if(!v[i])
            rec(i);
      }
      memset(v,0,sizeof(v));
      for(i=0;i<dagl;i++)
      {
         for(j=0;j<dag[i].size();j++)
         {
            a=dag[i][j];
            for(k=0;k<l;k++)
            {
               if(adj[a][k] && whichdag[k]!=i)
                  dagadj[i][whichdag[k]]=1;
            }
         }
      }
      lll=0;
      for(i=0;i<dagl;i++)
      {
         if(!v[i])
            dfs(i,1);
      }
      memset(v,0,sizeof(v));
      while(!pq.empty())
      {
         asd=pq.top();
         pq.pop();
         a=asd.id;
         if(!v[a])
         {   dfs(a,0);
            for(i=0;i<dag[a].size();i++)
               ans.push_back(arr[dag[a][i]]);
         }
      }
      sort(ans.begin(),ans.end());
      printf("%d\n",ans.size());
      for(i=0;i<ans.size();i++)
      {
         if(i)
            printf(" ");
         cout<<ans[i];
      }
      printf("\n");
      clear();
   }
   return 0;
}
Ronok1307
New poster
 
Posts: 8
Joined: Tue May 29, 2012 7:37 pm

Re: 1229 - Sub-dictionary (Why WA)

Postby mostafiz93 » Tue Feb 19, 2013 12:13 pm

There is one space character(' ') at the last of each input line in the sample input.
Should we consider this for every input?

Can anyone clarify this issue?
mostafiz93
New poster
 
Posts: 31
Joined: Thu Nov 24, 2011 12:08 am


Return to Volume XII

Who is online

Users browsing this forum: No registered users and 0 guests