Submission #1084218

# Submission time Handle Problem Language Result Execution time Memory
1084218 2024-09-05T16:18:38 Z vjudge1 KOVANICE (COI15_kovanice) C++17
50 / 100
2000 ms 63168 KB
#include <bits/stdc++.h>

using namespace std;

int prv_broj(string s)
{
    int x=0;
    for (int i=0;i<s.size();i++)
    {
        if (s[i]=='<' || s[i]=='=') return x;
        x = x*10 + (s[i]-'0');
    }
}

bool znak(string s)
{
    for (int i=0;i<s.size();i++)
        if (s[i]=='<') return true;
        else if (s[i]=='=') return false;
}

int vtor_broj(string s)
{
    int p=0;
    while(s[p]!='<' && s[p]!='=') p++;

    p++;

    int y=0;
    while(p<s.size())
    {
        y=y*10+(s[p]-'0');
        p++;
    }
    return y;
}

//vector<vector<int> > pocetno(1000000);
vector<pair<int,int> > v;
vector<vector<int> > ednakvi(1000000);
vector<vector<int> > pogolemi(1000000);
int pomali[300001];

int component[300001];
int c=0;
void dfs1(int poz)
{
    component[poz] = c;
    for (int i=0;i<ednakvi[poz].size();i++)
    {
        int sosed = ednakvi[poz][i];
        if (component[sosed]==0)
        {
            component[sosed] = c;
            dfs1(sosed);
        }
    }
}

void make_components(int m)
{
    for (int i=1;i<=m;i++)
        if (component[i]==0){
            c++;
            dfs1(i);
        }

    /*for (int i=1;i<=m;i++)
        cout<<"component["<<i<<"]="<<component[i]<<endl;*/
}

void make_vectors()
{
    for (int i=0;i<v.size();i++)
    {
        v[i].first = component[v[i].first];
        v[i].second = component[v[i].second];
    }

    sort(v.begin(),v.end());
    for (int i=0;i<v.size();i++)
    {
        int x = v[i].first;
        int y = v[i].second;
        pogolemi[x].push_back(y);
        pomali[y]++;
    }
}

int dp[300001],n;
bool dfs2(int poz,int depth)
{
    if (dp[poz]>0) return (depth == dp[poz]);
    if (pogolemi[poz].size()==0)
    {
        if (depth == n)
        {
            dp[poz] = depth;
            return true;
        }
        return false;
    }

    bool b = false;
    for (int i=0;i<pogolemi[poz].size();i++)
    {
        int sosed = pogolemi[poz][i];
        b = max(b,dfs2(sosed,depth+1));
    }
    if (b) dp[poz] = depth;
    return b;
}

void solve()
{
    for (int i=1;i<=c;i++)
    {
        if (pomali[i]==0)
        {
            dfs2(i,1);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m,k;
    cin>>n>>m>>k;
    for (int i=0;i<k;i++)
    {
        string s;
        cin>>s;

        int x = prv_broj(s);
        bool z = znak(s);
        int y = vtor_broj(s);
        //cout<<"epa "<<x <<" "<<z<<" "<<y<<endl;

        if (z)
        {
            //pocetno[x].push_back(y);
            v.push_back({x,y});
        }
        else{
            ednakvi[x].push_back(y);
            ednakvi[y].push_back(x);
        }
    }

    make_components(m);
    make_vectors();
    solve();

    for (int i=1;i<=m;i++)
    {
        int x = component[i];
        if (dp[x] == 0) cout<<"?\n";
        else cout<< "K"<<dp[x]<<endl;
    }

    return 0;
}

Compilation message

kovanice.cpp: In function 'int prv_broj(std::string)':
kovanice.cpp:8:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for (int i=0;i<s.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp: In function 'bool znak(std::string)':
kovanice.cpp:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for (int i=0;i<s.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp: In function 'int vtor_broj(std::string)':
kovanice.cpp:30:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     while(p<s.size())
      |           ~^~~~~~~~~
kovanice.cpp: In function 'void dfs1(int)':
kovanice.cpp:49:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for (int i=0;i<ednakvi[poz].size();i++)
      |                  ~^~~~~~~~~~~~~~~~~~~~
kovanice.cpp: In function 'void make_vectors()':
kovanice.cpp:74:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for (int i=0;i<v.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp:81:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i=0;i<v.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp: In function 'bool dfs2(int, int)':
kovanice.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int i=0;i<pogolemi[poz].size();i++)
      |                  ~^~~~~~~~~~~~~~~~~~~~~
kovanice.cpp: In function 'int prv_broj(std::string)':
kovanice.cpp:13:1: warning: control reaches end of non-void function [-Wreturn-type]
   13 | }
      | ^
kovanice.cpp: In function 'bool znak(std::string)':
kovanice.cpp:20:1: warning: control reaches end of non-void function [-Wreturn-type]
   20 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 22 ms 47448 KB Output is correct
2 Correct 32 ms 47708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 54860 KB Output is correct
2 Correct 166 ms 54988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2082 ms 49432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2066 ms 63168 KB Time limit exceeded
2 Halted 0 ms 0 KB -