답안 #1084231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084231 2024-09-05T16:31:11 Z vjudge1 KOVANICE (COI15_kovanice) C++17
100 / 100
264 ms 74844 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<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;
int doKraj[300001];
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;
    }
    if (doKraj[poz]>0 && doKraj[poz]+depth<n) 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));
        doKraj[poz]=max(doKraj[poz],doKraj[sosed]+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);

        if (z)
        {
            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:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for (int i=0;i<ednakvi[poz].size();i++)
      |                  ~^~~~~~~~~~~~~~~~~~~~
kovanice.cpp: In function 'void make_vectors()':
kovanice.cpp:73: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]
   73 |     for (int i=0;i<v.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp:80: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]
   80 |     for (int i=0;i<v.size();i++)
      |                  ~^~~~~~~~~
kovanice.cpp: In function 'bool dfs2(int, int)':
kovanice.cpp:106:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     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 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47448 KB Output is correct
2 Correct 21 ms 47452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 54172 KB Output is correct
2 Correct 173 ms 54436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 48644 KB Output is correct
2 Correct 33 ms 49392 KB Output is correct
3 Correct 37 ms 49404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 258 ms 62660 KB Output is correct
2 Correct 199 ms 66432 KB Output is correct
3 Correct 145 ms 65988 KB Output is correct
4 Correct 145 ms 65256 KB Output is correct
5 Correct 264 ms 74844 KB Output is correct