Submission #105830

# Submission time Handle Problem Language Result Execution time Memory
105830 2019-04-15T09:36:50 Z MohamedAhmed0 Cezar (COCI16_cezar) C++14
30 / 100
4 ms 384 KB
#include <bits/stdc++.h>

using namespace std;

vector< vector<int> >adj(28) ;
int vis[28] , st[28] , indegree[28];

bool prefix(string s , string s1)
{
    if(s.size() > s1.size())
        return false ;
    for(int i = 0 ; i < s.size() ; ++i)
    {
        if(s[i] != s1[i])
            return false ;
    }
    return true ;
}

bool cycle(int node)
{
    vis[node] = 1 ;
    st[node] = 1 ;
    for(auto &child : adj[node])
    {
        if(!vis[child])
        {
            if(cycle(child))
                return true ;
        }
        else if(st[child])
            return true ;
    }
    st[node] = 0 ;
    return false ;
}

int main()
{
    ios::sync_with_stdio(0) ;
    cin.tie(0) ;
    int n ;
    cin>>n ;
    string words[n] , arr[n];
    for(int i = 0 ; i < n ; ++i)
        cin>>words[i] ;
    for(int i = 0 ; i < n ; ++i)
    {
        int idx ;
        cin>>idx ;
        idx-- ;
        arr[i] = words[idx] ;
    }
    for(int i = 0 ; i < n ; ++i)
    {
        for(int j = 0 ; j < i ; ++j)
        {
            if(prefix(arr[i] , arr[j]))
                return cout<<"NE\n" , 0 ;
        }
    }
    //construct graph
    vector<int>v ;
    for(int i = 0 ; i < n-1 ; ++i)
    {
        for(int j = i+1 ; j < n ; ++j)
        {
            for(int k = 0 ; k < min(arr[i].size() , arr[j].size()) ; ++k)
            {
                if(arr[i][k] != arr[j][k])
                {
                    int c = arr[i][k] - 'a' , c2 = arr[j][k] - 'a' ;
                    adj[c].push_back(c2) ;
                    break;
                }
            }
        }
    }
    for(int i = 0 ; i < 26 ; ++i)
    {
        if(vis[i] == 0)
        {
            if(cycle(i))
                return cout<<"NE\n" , 0 ;
        }
    }
    cout<<"DA\n";
    memset(vis , 0 , sizeof(vis)) ;
    for(int i = 0 ; i < 26 ; ++i)
    {
        for(auto &child : adj[i])
            indegree[child]++ ;
    }
    vector<int>topo ;
    queue<int>q ;
    for(int i = 0 ; i < 26 ; ++i)
    {
        if(indegree[i] == 0)
        {
            vis[i] = 1 ;
            q.push(i) ;
        }
    }
    while(!q.empty())
    {
        int node = q.front() ;
        q.pop() ;
        topo.push_back(node) ;
        for(auto &child : adj[node])
        {
            if(vis[child])
                continue ;
            indegree[child]-- ;
            if(indegree[child] == 0)
            {
                q.push(child) ;
                vis[child] = 1 ;
            }
        }
    }
    string ans = string("" , 26) ;
    for(int i = 0 ; i < 26 ; ++i)
    {
        ans[topo[i]] = (char)(i+'a') ;
    }
    cout<<ans ;
    return 0 ;
}

Compilation message

cezar.cpp: In function 'bool prefix(std::__cxx11::string, std::__cxx11::string)':
cezar.cpp:12:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < s.size() ; ++i)
                     ~~^~~~~~~~~~
cezar.cpp: In function 'int main()':
cezar.cpp:68:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int k = 0 ; k < min(arr[i].size() , arr[j].size()) ; ++k)
                             ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -