提出 #68766271
ソースコード 拡げる
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define printar(ar) \
do \
{ \
for (auto dbg : ar) \
{ \
cout << dbg << " "; \
} \
cout << endl; \
} while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
if constexpr (idx < n)
return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
else
return init;
}
template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
return make_vec(d, T{});
}
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
size_t operator()(const std::pair<T, S> &keyval) const noexcept
{
return HashCombine(std::hash<T>()(keyval.first), keyval.second);
}
};
/* vector用 */
template <class T>
struct std::hash<std::vector<T>>
{
size_t operator()(const std::vector<T> &keyval) const noexcept
{
size_t s = 0;
for (auto &&v : keyval)
s = HashCombine(s, v);
return s;
}
};
/* tuple用 */
template <int N>
struct HashTupleCore
{
template <class Tuple>
size_t operator()(const Tuple &keyval) const noexcept
{
size_t s = HashTupleCore<N - 1>()(keyval);
return HashCombine(s, std::get<N - 1>(keyval));
}
};
template <>
struct HashTupleCore<0>
{
template <class Tuple>
size_t operator()(const Tuple &keyval) const noexcept { return 0; }
};
template <class... Args>
struct std::hash<std::tuple<Args...>>
{
size_t operator()(const tuple<Args...> &keyval) const noexcept
{
return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);
}
};
template <typename Type3>
class union_find
{
private:
vector<Type3> graph;
vector<Type3> sum;
public:
union_find(Type3 n)
{
graph.resize(n);
sum.resize(n);
for (int i = 0; i < n; i++)
{
graph[i] = i;
sum[i] = 1;
}
}
Type3 root(Type3 n)
{
if (graph[n] == n)
return n;
else
{
graph[n] = root(graph[n]);
return graph[n];
}
}
void debug()
{
for (unsigned i = 0; i < graph.size(); i++)
{
cout << graph[i] << " ";
}
cout << endl;
for (unsigned i = 0; i < graph.size(); i++)
{
cout << sum[i] << " ";
}
cout << endl;
}
void line(Type3 m, Type3 n)
{
if (check(m, n))
return;
sum[root(m)] += sum[root(n)];
sum[root(n)] = 0;
graph[root(n)] = root(m);
graph[n] = root(m);
}
Type3 size(Type3 n)
{
return sum[root(n)];
}
bool check(Type3 m, Type3 n)
{
return root(m) == root(n);
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, q;
cin >> n >> q;
union_find<ll> uf(n);
vector<bool> f(n);
vector<ll> c(n);
rep(i, 0, q)
{
ll op;
cin >> op;
if (op == 1)
{
ll u, v;
cin >> u >> v;
u--;
v--;
ll A = uf.root(u);
ll B = uf.root(v);
uf.line(u, v);
if (A != B)
{
c[uf.root(u)] = c[A] + c[B];
}
}
else if (op == 2)
{
ll v;
cin >> v;
v--;
if (f[v])
{
c[uf.root(v)]--;
}
else
{
c[uf.root(v)]++;
}
f[v] = !f[v];
}
else
{
ll v;
cin >> v;
v--;
if (c[uf.root(v)] > 0)
{
cout << "Yes" << Endl;
}
else
{
cout << "No" << Endl;
}
}
}
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | E - Reachability Query |
| ユーザ | a_s_k |
| 言語 | C++ 23 (gcc 12.2) |
| 得点 | 450 |
| コード長 | 5350 Byte |
| 結果 | AC |
| 実行時間 | 601 ms |
| メモリ | 7924 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 450 / 450 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | sample_01.txt |
| All | sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt, test_56.txt, test_57.txt, test_58.txt, test_59.txt, test_60.txt, test_61.txt, test_62.txt, test_63.txt, test_64.txt, test_65.txt, test_66.txt, test_67.txt, test_68.txt, test_69.txt, test_70.txt, test_71.txt, test_72.txt, test_73.txt, test_74.txt, test_75.txt, test_76.txt, test_77.txt, test_78.txt, test_79.txt, test_80.txt, test_81.txt, test_82.txt, test_83.txt, test_84.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| sample_01.txt | AC | 1 ms | 3448 KiB |
| test_01.txt | AC | 1 ms | 3340 KiB |
| test_02.txt | AC | 1 ms | 3340 KiB |
| test_03.txt | AC | 580 ms | 3440 KiB |
| test_04.txt | AC | 583 ms | 3456 KiB |
| test_05.txt | AC | 41 ms | 3428 KiB |
| test_06.txt | AC | 415 ms | 3568 KiB |
| test_07.txt | AC | 242 ms | 3436 KiB |
| test_08.txt | AC | 184 ms | 3516 KiB |
| test_09.txt | AC | 50 ms | 3552 KiB |
| test_10.txt | AC | 262 ms | 3436 KiB |
| test_11.txt | AC | 201 ms | 3416 KiB |
| test_12.txt | AC | 221 ms | 3472 KiB |
| test_13.txt | AC | 102 ms | 3512 KiB |
| test_14.txt | AC | 172 ms | 3476 KiB |
| test_15.txt | AC | 206 ms | 3504 KiB |
| test_16.txt | AC | 84 ms | 3628 KiB |
| test_17.txt | AC | 73 ms | 3452 KiB |
| test_18.txt | AC | 358 ms | 3504 KiB |
| test_19.txt | AC | 251 ms | 3548 KiB |
| test_20.txt | AC | 184 ms | 3472 KiB |
| test_21.txt | AC | 119 ms | 7836 KiB |
| test_22.txt | AC | 201 ms | 7828 KiB |
| test_23.txt | AC | 393 ms | 7776 KiB |
| test_24.txt | AC | 441 ms | 7688 KiB |
| test_25.txt | AC | 353 ms | 7816 KiB |
| test_26.txt | AC | 301 ms | 7804 KiB |
| test_27.txt | AC | 350 ms | 7748 KiB |
| test_28.txt | AC | 164 ms | 7772 KiB |
| test_29.txt | AC | 400 ms | 7744 KiB |
| test_30.txt | AC | 371 ms | 7756 KiB |
| test_31.txt | AC | 347 ms | 7744 KiB |
| test_32.txt | AC | 421 ms | 7752 KiB |
| test_33.txt | AC | 324 ms | 7756 KiB |
| test_34.txt | AC | 244 ms | 7772 KiB |
| test_35.txt | AC | 407 ms | 7920 KiB |
| test_36.txt | AC | 234 ms | 7840 KiB |
| test_37.txt | AC | 337 ms | 7812 KiB |
| test_38.txt | AC | 99 ms | 7764 KiB |
| test_39.txt | AC | 295 ms | 7804 KiB |
| test_40.txt | AC | 80 ms | 7744 KiB |
| test_41.txt | AC | 204 ms | 7840 KiB |
| test_42.txt | AC | 245 ms | 7764 KiB |
| test_43.txt | AC | 322 ms | 7752 KiB |
| test_44.txt | AC | 279 ms | 7840 KiB |
| test_45.txt | AC | 304 ms | 7832 KiB |
| test_46.txt | AC | 189 ms | 7800 KiB |
| test_47.txt | AC | 143 ms | 7904 KiB |
| test_48.txt | AC | 258 ms | 7840 KiB |
| test_49.txt | AC | 173 ms | 7752 KiB |
| test_50.txt | AC | 231 ms | 7840 KiB |
| test_51.txt | AC | 93 ms | 7836 KiB |
| test_52.txt | AC | 411 ms | 7796 KiB |
| test_53.txt | AC | 309 ms | 7800 KiB |
| test_54.txt | AC | 245 ms | 7756 KiB |
| test_55.txt | AC | 423 ms | 7744 KiB |
| test_56.txt | AC | 365 ms | 7840 KiB |
| test_57.txt | AC | 167 ms | 7748 KiB |
| test_58.txt | AC | 130 ms | 7776 KiB |
| test_59.txt | AC | 467 ms | 7924 KiB |
| test_60.txt | AC | 284 ms | 7796 KiB |
| test_61.txt | AC | 231 ms | 7832 KiB |
| test_62.txt | AC | 466 ms | 7768 KiB |
| test_63.txt | AC | 297 ms | 7764 KiB |
| test_64.txt | AC | 376 ms | 7832 KiB |
| test_65.txt | AC | 323 ms | 7728 KiB |
| test_66.txt | AC | 528 ms | 7812 KiB |
| test_67.txt | AC | 601 ms | 7744 KiB |
| test_68.txt | AC | 523 ms | 7812 KiB |
| test_69.txt | AC | 118 ms | 3724 KiB |
| test_70.txt | AC | 128 ms | 5632 KiB |
| test_71.txt | AC | 290 ms | 4132 KiB |
| test_72.txt | AC | 103 ms | 7508 KiB |
| test_73.txt | AC | 160 ms | 5720 KiB |
| test_74.txt | AC | 80 ms | 6700 KiB |
| test_75.txt | AC | 132 ms | 5376 KiB |
| test_76.txt | AC | 573 ms | 4852 KiB |
| test_77.txt | AC | 109 ms | 7796 KiB |
| test_78.txt | AC | 126 ms | 7776 KiB |
| test_79.txt | AC | 328 ms | 7748 KiB |
| test_80.txt | AC | 135 ms | 7684 KiB |
| test_81.txt | AC | 147 ms | 7688 KiB |
| test_82.txt | AC | 262 ms | 7804 KiB |
| test_83.txt | AC | 115 ms | 7832 KiB |
| test_84.txt | AC | 351 ms | 7752 KiB |