Submission #758674

# Submission time Handle Problem Language Result Execution time Memory
758674 2023-06-15T05:42:17 Z boris_mihov Simurgh (IOI17_simurgh) C++17
0 / 100
19 ms 6760 KB
#include "simurgh.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <set>

typedef long long llong;
const int MAXN = 500 + 10;
const int MAXM = 30000;
const int INF  = 1e9;

int n, m;
int timer;
int inComp[MAXN];
std::set <int> s;
std::vector <int> ans;
std::vector <int> spanning;
std::vector <std::pair <int,int>> edges;
std::vector <std::pair <int,int>> g[MAXN];
bool isGood[MAXM];

void dfs(int node)
{
    inComp[node] = timer;
    for (const auto &[i, idx] : g[node])
    {
        if (inComp[i] == 0)
        {
            spanning.push_back(idx);
            dfs(i);
        }
    }
}

std::vector <int> find_roads(int N, std::vector <int> u, std::vector <int> v)
{
	n = N;
    m = u.size();
    for (int i = 0 ; i < m ; ++i)
    {
        g[u[i] + 1].push_back({v[i] + 1, i});
        g[v[i] + 1].push_back({u[i] + 1, i});
    }

    int last = -1;
    int res = -1;

    for (int i = 1 ; i <= n ; ++i)
    {
        timer = 0;
        spanning.clear();
        std::fill(inComp + 1, inComp + 1 + n, 0);
        bool didntPush = true;
        inComp[i] = -1;

        for (const int &j : ans)
        {
            isGood[j] = true;
        }

        for (int j = 1 ; j <= n ; ++j)
        {
            if (inComp[j] != 0)
            {
                continue;
            }

            timer++;
            dfs(j);
        }

        std::sort(g[i].begin(), g[i].end(), [&](auto x, auto y)
        {
            return inComp[x.first] < inComp[y.first];
        });

        int curr = spanning.size();
        for (int j = 0 ; j < g[i].size() ; ++j)
        {
            if (j == 0 || inComp[g[i][j - 1].first] < inComp[g[i][j].first])
            {
                spanning.push_back(g[i][j].second);
            }
        }

        for (int j = 0 ; j <= g[i].size() ; ++j)
        {
            if (j == g[i].size() || (j > 0 && inComp[g[i][j - 1].first] < inComp[g[i][j].first]))
            {
                assert (edges.size());
                std::sort(edges.begin(), edges.end(), [&](auto x, auto y)
                {
                    return x.second < y.second;
                });

                ans.push_back(edges.back().first);
                // std::cout << "in for: " << edges.back().first << ' ' << edges.back().second << '\n';
                for (int k = (int)edges.size() - 2 ; k >= 0 ; --k)
                {
                    if (edges[k].second != edges[k + 1].second)
                    {
                        // std::cout << "break: " << edges[k].second << ' ' << edges[k + 1].second << '\n';
                        break;
                    }

                    // std::cout << "in for: " << edges[k].first << ' ' << edges[k].second << ' ' << edges[k + 1].second << '\n';
                    ans.push_back(edges[k].first);
                }

                curr++;
                edges.clear();
                if (j == g[i].size())
                {
                    break;
                }
            }

            if (i < g[i][j].first)
            {
                spanning[curr] = g[i][j].second;
                res = count_common_roads(spanning);
                // std::cout << "push: " << edges.size() << ' ' << g[i][j].second << ' ' << res << '\n';
                edges.push_back({g[i][j].second, res});
            } else if (didntPush && isGood[g[i][j].second])
            {
                didntPush = false;
                spanning[curr] = g[i][j].second;
                res = count_common_roads(spanning);
                // std::cout << "push: " << edges.size() << ' ' << g[i][j].second << ' ' << res << '\n';
                edges.push_back({g[i][j].second, res});
            }
        }
    }

    for (const int &i : ans)
    {
        s.insert(i);
    }

    ans.clear();
    for (const int &i : s)
    {
        ans.push_back(i);
    }

    return ans;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:80:28: 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 j = 0 ; j < g[i].size() ; ++j)
      |                          ~~^~~~~~~~~~~~~
simurgh.cpp:88:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for (int j = 0 ; j <= g[i].size() ; ++j)
      |                          ~~^~~~~~~~~~~~~~
simurgh.cpp:90: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]
   90 |             if (j == g[i].size() || (j > 0 && inComp[g[i][j - 1].first] < inComp[g[i][j].first]))
      |                 ~~^~~~~~~~~~~~~~
simurgh.cpp:114:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |                 if (j == g[i].size())
      |                     ~~^~~~~~~~~~~~~~
simurgh.cpp:47:9: warning: unused variable 'last' [-Wunused-variable]
   47 |     int last = -1;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Runtime error 1 ms 468 KB Execution killed with signal 6
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Runtime error 1 ms 468 KB Execution killed with signal 6
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Runtime error 1 ms 468 KB Execution killed with signal 6
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Runtime error 19 ms 6760 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Runtime error 1 ms 468 KB Execution killed with signal 6
10 Halted 0 ms 0 KB -