답안 #825334

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
825334 2023-08-14T17:54:38 Z HorizonWest 슈퍼트리 잇기 (IOI20_supertrees) C++17
21 / 100
180 ms 24148 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;

        if(s.size() > 0)
        {
            ans[x][s[0]] = 1;
            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; s.push_back(i);
        map <int, int> mk; mk[i] = 1;

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

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

        if(m != 1)
        {
            if(m == 2) return 0;

            for(int j = 0; j < m; j++)
            {
                for(int k = 0; k < n; k++)
                {
                    if(((p[s[j]][k] != 0) ^ (mk[k])))
                        return 0;
                }
            }

            for(int j = 0; j < m; 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:138:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |             for(int j = 0; j < g[i].size(); j++)
      |                            ~~^~~~~~~~~~~~~
supertrees.cpp:146:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |             for(int j = 0; j < s.size(); j++)
      |                            ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 174 ms 24040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 174 ms 24040 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1308 KB Output is correct
13 Correct 161 ms 24064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 820 KB Output is correct
17 Correct 74 ms 14064 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6288 KB Output is correct
21 Correct 154 ms 24012 KB Output is correct
22 Correct 180 ms 24080 KB Output is correct
23 Correct 160 ms 24008 KB Output is correct
24 Correct 161 ms 24096 KB Output is correct
25 Correct 65 ms 14132 KB Output is correct
26 Correct 66 ms 14068 KB Output is correct
27 Correct 157 ms 23996 KB Output is correct
28 Correct 157 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Answer gives possible 0 while actual possible 1
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Answer gives possible 0 while actual possible 1
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 174 ms 24040 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1308 KB Output is correct
13 Correct 161 ms 24064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 820 KB Output is correct
17 Correct 74 ms 14064 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6288 KB Output is correct
21 Correct 154 ms 24012 KB Output is correct
22 Correct 180 ms 24080 KB Output is correct
23 Correct 160 ms 24008 KB Output is correct
24 Correct 161 ms 24096 KB Output is correct
25 Correct 65 ms 14132 KB Output is correct
26 Correct 66 ms 14068 KB Output is correct
27 Correct 157 ms 23996 KB Output is correct
28 Correct 157 ms 24148 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Incorrect 0 ms 212 KB Answer gives possible 0 while actual possible 1
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 174 ms 24040 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1308 KB Output is correct
13 Correct 161 ms 24064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 820 KB Output is correct
17 Correct 74 ms 14064 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6288 KB Output is correct
21 Correct 154 ms 24012 KB Output is correct
22 Correct 180 ms 24080 KB Output is correct
23 Correct 160 ms 24008 KB Output is correct
24 Correct 161 ms 24096 KB Output is correct
25 Correct 65 ms 14132 KB Output is correct
26 Correct 66 ms 14068 KB Output is correct
27 Correct 157 ms 23996 KB Output is correct
28 Correct 157 ms 24148 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Incorrect 0 ms 212 KB Answer gives possible 0 while actual possible 1
34 Halted 0 ms 0 KB -