Submission #825309

# Submission time Handle Problem Language Result Execution time Memory
825309 2023-08-14T17:22:37 Z HorizonWest Connecting Supertrees (IOI20_supertrees) C++17
0 / 100
1 ms 340 KB
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define db double
#define ll __int128
//#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;

const int Max = 1e6 + 7, Inf = 1e9 + 7;

void build(std::vector<std::vector<int>> b);

struct DSU
{
    vector <int> link, size;

    int find(int x)
    {
        while(x != link[x]) x = link[x];
        return x;
    }

    int sz(int x)
    {
        return size[find(x)];
    }

    bool same(int a, int b)
    {
        return find(a) == find(b);
    }

    void unite(int a, int b)
    {
        a = find(a); b = find(b);
        if(a == b) return;
        if(size[a] < size[b]) swap(a, b);
        link[b] = a; size[a] += size[b];
    }

    DSU(int n)
    {
        link.assign(n+1, 0);
        size.assign(n+1, 1);
        for(int i = 0; i <= n; i++)
            link[i] = i;
    }
};

int construct(std::vector<std::vector<int>> p)
{
    int n = p.size(); DSU St(n);

    vector <vector<int>> g, ans(n, vector <int> (n, 0));

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++) if(j != i)
        {
            if(p[i][j] != p[j][i])
                return 0;

            if(p[i][j] == 1)
                St.unite(i, j);
        }
    }

    map <int, int> mp, pass; int idx = 0;

    for(int i = 0; i < n; i++)
    {
        int x = St.find(i);
        if(mp[x] == 0)
        {
            idx++;
            mp[x] = idx;
            g.push_back(vector <int> ());
        }
        g[mp[x]-1].push_back(i);
    }

    for(int i = 0; i < (int) g.size(); i++)
    {
        for(int j = 0; j < g[i].size(); j++)
        {
            for(int k = j+1; k < g[i].size(); k++)
            {
                if(p[g[i][j]][g[i][k]] != 1)
                    return 0;
            }
            pass[g[i][j]] = i+1;
        }

        for(int j = 0; j < g[i].size() - 1; j++)
        {
            ans[g[i][j]][g[i][j+1]] = 1;
            ans[g[i][j+1]][g[i][j]] = 1;
        }

        int x = g[i][0]; vector <int> s;

        for(int j = 0; j < n; j++)
        {
            if(p[x][j] == 2)
            {
                if(pass[j] != 0)
                    return 0;
                pass[j] = i+1;
                s.push_back(j);
                St.unite(x, j);
            }
        }

        if(s.size() > 0 && s.size() < 3) return 0;
        ans[x][s[0]] = ans[s[0]][x] = 1;

        int mod = s.size();

        for(int j = 0; j < (int) s.size(); j++)
        {
            ans[s[j]][s[(j+1) % mod]] = 1;
            ans[s[(j+1) % mod]][s[j]] = 1;
        }

        for(int j = 0; j < g[i].size(); j++)
        {
            for(int k = 0; k < n; k++) if(p[g[i][j]][k] != 0)
            {
                if(pass[k] != i + 1) return 0;
            }
        }

        for(int j = 0; j < s.size(); j++)
        {
            for(int k = 0; k < n; k++) if(p[s[j]][k] != 0)
            {
                if(pass[k] != i + 1) return 0;
            }
        }
    }

    for(int i = 0; i < n; i++) if(St.sz(i) == 1)
    {
        vector <int> s;

        for(int j = i+1; j < n; j++) if(p[i][j] == 2)
        {
            s.push_back(j);
            St.unite(i, j);
        }

        int m = (int) s.size();

        for(int j = 0; j < m; j++)
        {
            for(int k = 0; k < n; k++)
            {
                if(p[s[j]][k] != 0 && !St.same(s[j], k))
                    return 0;
            }
        }

        if(m == 2) return 0;

        for(int j = 0; j < (int) s.size(); j++)
        {
            ans[s[j]][s[(j+1) % m]] = 1;
            ans[s[(j+1) % m]][s[j]] = 1;
        }
    }

    build(ans);
    return 1;
}









Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int j = 0; j < g[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~
supertrees.cpp:95:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             for(int k = j+1; k < g[i].size(); k++)
      |                              ~~^~~~~~~~~~~~~
supertrees.cpp:103:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for(int j = 0; j < g[i].size() - 1; j++)
      |                        ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:134:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |         for(int j = 0; j < g[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~
supertrees.cpp:142:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |         for(int j = 0; j < s.size(); j++)
      |                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 340 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -