提出 #64760216
ソースコード 拡げる
#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1
#include <algorithm>
#include <cassert>
#include <vector>
namespace atcoder {
// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
public:
dsu() : _n(0) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}
private:
int _n;
// root node: -1 * component size
// otherwise: parent
std::vector<int> parent_or_size;
};
} // namespace atcoder
#endif // ATCODER_DSU_HPP
#ifndef IO_HPP
#define IO_HPP 1
#include <array>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
namespace io {
void __read(char& c) { std::cin >> c; }
void __read(std::string& s) { std::cin >> s; }
template <typename T>
void __read_real(T& x) {
std::string s;
__read(s);
x = std::stod(s);
}
template <typename T>
void __read_integer(T& x) {
std::cin >> x;
}
void __read(int& x) { __read_integer(x); }
void __read(unsigned int& x) { __read_integer(x); }
void __read(long& x) { __read_integer(x); }
void __read(unsigned long& x) { __read_integer(x); }
void __read(long long& x) { __read_integer(x); }
void __read(unsigned long long& x) { __read_integer(x); }
void __read(double& x) { __read_real(x); }
void __read(long double& x) { __read_real(x); }
template <class U, class V>
void __read(std::pair<U, V>& p) {
__read(p.first);
__read(p.second);
}
template <size_t N = 0, typename T>
void __read_tuple(T& t) {
if constexpr (N < std::tuple_size<T>::value) {
auto& x = std::get<N>(t);
__read(x);
__read_tuple<N + 1>(t);
}
}
template <class... T>
void __read(std::tuple<T...>& t) {
__read_tuple(t);
}
template <size_t N = 0, typename T>
void __read(std::array<T, N>& a) {
for (auto& x : a) {
__read(x);
}
}
template <class T>
void __read(std::vector<T>& v) {
for (auto& x : v) {
__read(x);
}
}
void read() {}
template <class H, class... T>
void read(H& h, T&... t) {
__read(h), read(t...);
}
void __write(const char c) { std::cout << c; }
void __write(const std::string s) {
for (char c : s) {
__write(c);
}
}
void __write(const char* s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; ++i) {
__write(s[i]);
}
}
template <typename T>
void __write_integer(T x) {
std::cout << x;
}
template <typename T>
void __write_real(T x) {
std::ostringstream oss;
oss << std::fixed << std::setprecision(15) << double(x);
std::string s = oss.str();
__write(s);
}
void __write(int x) { __write_integer(x); }
void __write(unsigned int x) { __write_integer(x); }
void __write(long x) { __write_integer(x); }
void __write(unsigned long x) { __write_integer(x); }
void __write(long long x) { __write_integer(x); }
void __write(unsigned long long x) { __write_integer(x); }
void __write(double x) { __write_real(x); }
void __write(long double x) { __write_real(x); }
template <class U, class V>
void __write(const std::pair<U, V> p) {
__write(p.first);
__write(' ');
__write(p.second);
}
template <size_t N = 0, typename T>
void __write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) {
__write(' ');
}
const auto x = std::get<N>(t);
__write(x);
__write_tuple<N + 1>(t);
}
}
template <class... T>
void __write(std::tuple<T...> t) {
__write_tuple(t);
}
template <class T, size_t S>
void __write(const std::array<T, S> a) {
auto n = a.size();
for (size_t i = 0; i < n; i++) {
if (i) {
__write(' ');
}
__write(a[i]);
}
}
template <class T>
void __write(const std::vector<T> v) {
auto n = v.size();
for (size_t i = 0; i < n; i++) {
if (i) {
__write(' ');
}
__write(v[i]);
}
}
template <class T>
void __write(const std::set<T> s) {
__write(std::vector<T>{s.cbegin(), s.cend()});
}
template <class K, class V>
void __write(const std::map<K, V> m) {
__write(std::vector<std::pair<K, V>>{m.cbegin(), m.cend()});
}
void write() { __write('\n'); }
template <class Head, class... Tail>
void write(Head&& head, Tail&&... tail) {
__write(head);
if (sizeof...(Tail)) {
__write(' ');
}
write(std::forward<Tail>(tail)...);
}
} // namespace io
#endif // IO_HPP
#ifdef ONLINE_JUDGE
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops,inline")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#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;
using namespace atcoder;
using namespace io;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve() {
int n, m;
read(n, m);
vector<pair<int, int>> uv(m);
read(uv);
vector<vector<int>> g(n);
for (auto& [u, v] : uv) {
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
int k{};
set<int> op;
dsu cc(n);
for (int u{}; u < n; ++u) {
++k;
op.erase(u);
for (int v : g[u]) {
if (u < v) {
op.insert(v);
} else {
k -= !cc.same(u, v);
cc.merge(u, v);
}
}
write(k > 1 ? -1 : int{op.size()});
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
solve();
}
提出情報
コンパイルエラー
Main.cpp: In function ‘void solve()’:
Main.cpp:310:35: warning: narrowing conversion of ‘op.std::set<int>::size()’ from ‘std::set<int>::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
310 | write(k > 1 ? -1 : int{op.size()});
| ~~~~~~~^~
Main.cpp:310:35: warning: narrowing conversion of ‘op.std::set<int>::size()’ from ‘std::set<int>::size_type’ {aka ‘long unsigned int’} to ‘int’ [-Wnarrowing]
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
450 / 450 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
| All |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
1 ms |
3500 KiB |
| 00_sample_01.txt |
AC |
1 ms |
3380 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3520 KiB |
| 00_sample_03.txt |
AC |
1 ms |
3524 KiB |
| 01_random_04.txt |
AC |
59 ms |
16260 KiB |
| 01_random_05.txt |
AC |
117 ms |
26620 KiB |
| 01_random_06.txt |
AC |
84 ms |
21056 KiB |
| 01_random_07.txt |
AC |
98 ms |
26624 KiB |
| 01_random_08.txt |
AC |
166 ms |
21820 KiB |
| 01_random_09.txt |
AC |
115 ms |
20008 KiB |
| 01_random_10.txt |
AC |
154 ms |
21996 KiB |
| 01_random_11.txt |
AC |
147 ms |
21680 KiB |
| 01_random_12.txt |
AC |
163 ms |
21864 KiB |
| 01_random_13.txt |
AC |
147 ms |
21868 KiB |
| 01_random_14.txt |
AC |
158 ms |
21860 KiB |
| 01_random_15.txt |
AC |
50 ms |
8664 KiB |
| 01_random_16.txt |
AC |
21 ms |
8108 KiB |
| 01_random_17.txt |
AC |
9 ms |
8060 KiB |
| 01_random_18.txt |
AC |
2 ms |
4076 KiB |
| 01_random_19.txt |
AC |
3 ms |
4140 KiB |
| 01_random_20.txt |
AC |
24 ms |
7420 KiB |
| 01_random_21.txt |
AC |
15 ms |
5168 KiB |
| 01_random_22.txt |
AC |
5 ms |
3828 KiB |
| 01_random_23.txt |
AC |
31 ms |
8600 KiB |
| 01_random_24.txt |
AC |
31 ms |
7796 KiB |
| 01_random_25.txt |
AC |
17 ms |
5744 KiB |
| 01_random_26.txt |
AC |
111 ms |
19964 KiB |
| 01_random_27.txt |
AC |
140 ms |
19668 KiB |
| 01_random_28.txt |
AC |
121 ms |
19992 KiB |
| 01_random_29.txt |
AC |
142 ms |
19732 KiB |
| 01_random_30.txt |
AC |
116 ms |
19880 KiB |
| 01_random_31.txt |
AC |
137 ms |
19756 KiB |
| 01_random_32.txt |
AC |
129 ms |
20048 KiB |
| 01_random_33.txt |
AC |
135 ms |
19744 KiB |
| 01_random_34.txt |
AC |
112 ms |
19964 KiB |
| 01_random_35.txt |
AC |
123 ms |
19736 KiB |
| 01_random_36.txt |
AC |
124 ms |
20148 KiB |
| 01_random_37.txt |
AC |
135 ms |
19748 KiB |
| 01_random_38.txt |
AC |
1 ms |
3452 KiB |
| 01_random_39.txt |
AC |
1 ms |
3448 KiB |
| 01_random_40.txt |
AC |
1 ms |
3508 KiB |
| 01_random_41.txt |
AC |
1 ms |
3416 KiB |
| 01_random_42.txt |
AC |
1 ms |
3576 KiB |
| 01_random_43.txt |
AC |
1 ms |
3508 KiB |
| 01_random_44.txt |
AC |
1 ms |
3528 KiB |
| 01_random_45.txt |
AC |
1 ms |
3424 KiB |
| 01_random_46.txt |
AC |
1 ms |
3444 KiB |
| 01_random_47.txt |
AC |
1 ms |
3644 KiB |
| 01_random_48.txt |
AC |
1 ms |
3444 KiB |