답안 #640889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
640889 2022-09-15T13:35:55 Z pls33 Monthly railway pass (LMIO18_menesinis_bilietas) C++17
100 / 100
250 ms 30768 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#pragma region dalykai
using p32 = pair<int, int>;
using p32u = pair<uint32_t, uint32_t>;
using p64 = pair<int64_t, int64_t>;
using p64u = pair<uint64_t, uint64_t>;
using vi16 = vector<int16_t>;
using vi16u = vector<uint16_t>;
using vi32 = vector<int>;
using vi32u = vector<uint32_t>;
using vi64 = vector<int64_t>;
using vi64u = vector<uint64_t>;
using vp32 = vector<p32>;
using vp32u = vector<p32u>;
using vp64 = vector<p64>;
using vp64u = vector<p64u>;
using vvi32 = vector<vi32>;
using vvi32u = vector<vi32u>;
using vvi64 = vector<vi64>;
using vvi64u = vector<vi64u>;
using vvp32 = vector<vp32>;
using vvp32u = vector<vp32u>;
using vvp64 = vector<vp64>;
using vvp64u = vector<vp64u>;

using pf32 = pair<float, float>;
using pf64 = pair<double, double>;
using pf80 = pair<long double, long double>;
using vf32 = vector<float>;
using vf64 = vector<double>;
using vf80 = vector<long double>;
using vpf32 = vector<pf32>;
using vpf64 = vector<pf64>;
using vpf80 = vector<pf80>;
using vvf32 = vector<vf32>;
using vvf64 = vector<vf64>;
using vvf80 = vector<vf80>;
using vvpf32 = vector<vpf32>;
using vvpf64 = vector<vpf64>;
using vvpf80 = vector<vpf80>;

template <typename key, typename val>
using ord_map = tree<key, val, less<key>, rb_tree_tag,
                     tree_order_statistics_node_update>;

template <typename key>
using ord_set = tree<key, null_type, less<key>, rb_tree_tag,
                     tree_order_statistics_node_update>;

const int BUF_SZ = 1 << 15;
inline namespace fast_in
{
    char buf[BUF_SZ];
    int pos;
    int len;

    char next_char(FILE *f)
    {
        if (pos == len)
        {
            pos = 0;
            len = (int)fread(buf, 1, BUF_SZ, f);

            if (!len)
            {
                return EOF;
            }
        }

        return buf[pos++];
    }

    int read_int(FILE *f)
    {
        int x;
        char ch;
        int sgn = 1;

        while (!isdigit(ch = next_char(f)))
        {
            if (ch == '-')
            {
                sgn *= -1;
            }
        }

        x = ch - '0';
        while (isdigit(ch = next_char(f)))
        {
            x = x * 10 + (ch - '0');
        }

        return x * sgn;
    }
}

/**
 * @brief gale programos flush_out kviest!!
 */
inline namespace fast_out
{
    char buf[BUF_SZ];
    int pos;

    void flush_out(FILE *f)
    {
        fwrite(buf, 1, pos, f);
        pos = 0;
    }

    void write_char(char c, FILE *f)
    {
        if (pos == BUF_SZ)
        {
            flush_out(f);
        }

        buf[pos++] = c;
    }

    void write_int(int x, FILE *f)
    {
        static char num_buf[100];

        if (x < 0)
        {
            write_char('-', f);
            x *= -1;
        }

        int len = 0;
        for (; x >= 10; x /= 10)
        {
            num_buf[len++] = (char)('0' + (x % 10));
        }

        write_char((char)('0' + x), f);
        while (len)
        {
            write_char(num_buf[--len], f);
        }
        write_char('\n', f);
    }
}
#pragma endregion

struct dsu_t
{
    vi32 parent, rank;

    dsu_t(int n) : parent(n), rank(n, 1)
    {
        iota(parent.begin(), parent.end(), 0);
    }

    int find(int i)
    {
        if (parent[i] == i)
        {
            return i;
        }

        return parent[i] = find(parent[i]);
    }

    void set_union(int a, int b)
    {
        a = find(a);
        b = find(b);
        if (a == b)
        {
            return;
        }

        if (rank[b] > rank[a])
        {
            swap(a, b);
        }

        parent[b] = a;
        rank[a] += rank[b];
    }
};

int main()
{
#ifndef _AAAAAAAAA
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#else
    freopen("bilietas.in", "r", stdin);
#ifndef __linux__
    atexit([]()
           {
        freopen("con", "r", stdin);
        system("pause"); });
#endif
#endif

    int v, e;
    cin >> v >> e;

    dsu_t dsu(v);
    vp32 bus;
    for (size_t i = 0; i < e; i++)
    {
        int a, b;
        char type;
        cin >> a >> b >> type;

        a--;
        b--;

        if (type == 'A')
        {
            bus.emplace_back(a, b);
        }
        else
        {
            dsu.set_union(a, b);
        }
    }

    vvi32 adj(v);
    bool used_bus = true;
    for (auto &b : bus)
    {
        b.first = dsu.find(b.first);
        b.second = dsu.find(b.second);
        if (b.first == b.second)
        {
            continue;
        }

        used_bus = false;
        adj[b.first].push_back(b.second);
        adj[b.second].push_back(b.first);
    }

    if (used_bus)
    {
        bool connected = true;
        for (auto &p : dsu.parent)
        {
            if (dsu.find(p) != dsu.parent.front())
            {
                connected = false;
                break;
            }
        }

        v = connected ? v : 0;
        cout << v << '\n';

        return 0;
    }

    int count = 0;
    for (int i = 0; i < adj.size(); i++)
    {
        auto &next = adj[i];
        if (next.size())
        {
            sort(next.begin(), next.end());

            auto it = unique(next.begin(), next.end());
            next.erase(it, next.end());

            int cur = dsu.rank[i];
            for (auto &a : next)
            {
                cur += dsu.rank[a];
            }

            count += (cur == v) * dsu.rank[i];
        }
    }

    cout << count << '\n';

    return 0;
}

Compilation message

menesinis_bilietas.cpp:8: warning: ignoring '#pragma region dalykai' [-Wunknown-pragmas]
    8 | #pragma region dalykai
      | 
menesinis_bilietas.cpp:151: warning: ignoring '#pragma endregion ' [-Wunknown-pragmas]
  151 | #pragma endregion
      | 
menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:211:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  211 |     for (size_t i = 0; i < e; i++)
      |                        ~~^~~
menesinis_bilietas.cpp:265:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  265 |     for (int i = 0; i < adj.size(); i++)
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 9804 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 8 ms 14932 KB Output is correct
5 Correct 1 ms 708 KB Output is correct
6 Correct 30 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14932 KB Output is correct
2 Correct 1 ms 708 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 110 ms 8748 KB Output is correct
7 Correct 250 ms 30768 KB Output is correct
8 Correct 5 ms 1620 KB Output is correct
9 Correct 11 ms 1872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 340 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 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 592 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 1 ms 708 KB Output is correct
22 Correct 5 ms 1620 KB Output is correct
23 Correct 11 ms 1872 KB Output is correct
24 Correct 30 ms 2892 KB Output is correct
25 Correct 8 ms 1236 KB Output is correct
26 Correct 94 ms 11720 KB Output is correct
27 Correct 36 ms 4832 KB Output is correct
28 Correct 53 ms 6356 KB Output is correct
29 Correct 29 ms 3796 KB Output is correct
30 Correct 59 ms 5388 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 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 592 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 8 ms 1236 KB Output is correct
17 Correct 94 ms 11720 KB Output is correct
18 Correct 36 ms 4832 KB Output is correct
19 Correct 53 ms 6356 KB Output is correct
20 Correct 29 ms 3796 KB Output is correct
21 Correct 59 ms 5388 KB Output is correct
22 Correct 96 ms 9804 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 8 ms 14932 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 1 ms 708 KB Output is correct
30 Correct 110 ms 8748 KB Output is correct
31 Correct 250 ms 30768 KB Output is correct
32 Correct 5 ms 1620 KB Output is correct
33 Correct 11 ms 1872 KB Output is correct
34 Correct 30 ms 2892 KB Output is correct
35 Correct 18 ms 2744 KB Output is correct
36 Correct 127 ms 15736 KB Output is correct
37 Correct 59 ms 9676 KB Output is correct