結果
| 問題 | No.2403 "Eight" Bridges of Königsberg |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-04 22:50:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 13,114 bytes |
| コンパイル時間 | 2,254 ms |
| コンパイル使用メモリ | 226,804 KB |
| 最終ジャッジ日時 | 2025-02-15 23:09:24 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 21 WA * 10 |
ソースコード
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
//@formatter:off
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
//@formatter:off
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<ll, ll>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VVVVI = vector<VVVI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VVVVL = vector<VVVL>;
using VP = vector<P>;
using VVP = vector<vector<P>>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
using VD = vector<double>;
using VVD = vector<vector<double>>;
using VVVD = vector<VVD>;
using I_I = pair<int, int>;
const int INF = (int)1e9 + 10; // int max > 2*10^9
const long long INFL = (ll)2e18 + 10; // ll max > 9*10^18
const int JU_5 = 100000;
const int JU_6 = 1000000;
const ll JU_9 = 1000000000;
const ll JU_18 = JU_9*JU_9;
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
template <typename T, typename C>
pair<T,C> operator+(const pair<T,C> &a, const pair<T,C> &b) { return make_pair(a.first + b.first, a.second + b.second); }
template <typename T, typename C>
pair<T,C> operator-(const pair<T,C> &a, const pair<T,C> &b) { return make_pair(a.first - b.first, a.second - b.second); }
template <typename T, typename C>
pair<T,C> operator*(const pair<T,C> &a, const pair<T,C> &b) { return make_pair(a.first * b.first, a.second * b.second); }
template <typename T, typename C>
pair<T,C> operator/(const pair<T,C> &a, const pair<T,C> &b) { return make_pair(a.first / b.first, a.second / b.second); }
// #define MOD 1000000007
#define MOD 998244353
#define pmod(x,y) ((x%y)+y)%y
/********** libs **********/
//@formatter:off
// jhelper 用変数
ostream *__cout;
istream *__cin;
void _init_io(std::istream &cin, std::ostream &cout) {
// 入出力高速化用
ios::sync_with_stdio(false); cin.tie(nullptr);
cout << std::setprecision(30);
// jhelper 用
__cout = &cout; __cin = &cin;
}
//@formatter:off
/* デバッグ用 出力 */
template<typename T>
ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v: vec) os << v << ","; os << "]"; return os; }
template<typename T>
ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v: vec) os << v << ","; os << "}"; return os; }
template<typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v: vec) os << v << ","; os << "}"; return os; }
template<typename T>
ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v: vec) os << v << ","; os << "}"; return os; }
template<typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v: vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v: mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v: mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
// print like python
// src: https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { *__cout << '\n'; }
template<class Head, class... Tail>
void print(Head &&head, Tail &&... tail) { *__cout << head; if (sizeof...(tail) != 0) *__cout << " "; print(forward<Tail>(tail)...); }
template<class T>
void print(vector<T> &vec) { for (auto &a: vec) { *__cout << a; if (&a != &vec.back()) *__cout << " "; } *__cout << '\n'; }
template<class T>
void print(vector<vector<T>> &df) { for (auto &vec: df) { print(vec); } }
// endl なし
void Print() {}
template<class Head, class... Tail>
void Print(Head &&head, Tail &&... tail) { *__cout << head; if (sizeof...(tail) != 0) *__cout << " "; Print(forward<Tail>(tail)...); }
template<class T>
void Print(vector<T> &vec) { for (auto &a: vec) { *__cout << a; if (&a != &vec.back()) *__cout << " "; } }
//@formatter:off
/*
* org: https://atcoder.jp/contests/abc213/submissions/24901856
* jhelper で使用するため, 全部 cin での入力にしている. (現状のレベルでは困らないので)
*/
template<class T> void scan(T& a){ *__cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
void in(){}
template <class... T> void in(T&... a){ (void)initializer_list<int>{ (scan(a), 0)... }; }
#define let_v(type,name,...) vector<type>name(__VA_ARGS__)
#define let_V(type,name,size) vector<type>name(size);in(name)
#define let_vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define let_VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define let_vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define let_vvvv(type,name,h,w,x,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(x,vector<type>(__VA_ARGS__))))
/* string 入力を vector<char> で受け取るみたいな */
vector<char> let_CharVec() { string s; *__cin >> s; return {s.begin(), s.end()}; }
//@formatter:off
//ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T, class C>
bool chmax(T& a, C b) { if (a < b) { a = b; return true; } return false; }
template <class T, class C>
bool chmin(T& a, C b) { if (a > b) { a = b; return true; } return false; }
template <class T>
T sum(const vector<T>& v) { T res = 0; for (size_t i = 0; i < v.size(); ++i) res += v[i]; return res; }
//template <class T, class C>
//T min(T a, C b) { return a <= b ? a : T(b); }
//
//template <class T, class C>
//T max(T a, C b) { return a >= b ? a : T(b); }
template<class T>
vector<pair<T, int>> idx_pair(vector<T> &vals) { vector<pair<T, int>> ans; rep(i, vals.size()) { ans.emplace_back(vals[i], i); } return ans; }
template<typename T>
int argmax(vector<T> &vals){ if(vals.size()==0)return -1; int res=0; for(int i = 1;i<vals.size();i++){ if(vals[i]>vals[res])res=i; } return res; }
template<typename T>
int argmin(vector<T> &vals){ if(vals.size()==0)return -1; int res=0; for(int i = 1;i<vals.size();i++){ if(vals[i]<vals[res])res=i; } return res; }
// add other source
struct UnionFind {
// data 中の負数の要素が根となる
// 根は集合のデータ数を負数でもつ
vector<int> data;
UnionFind(int size) : data(size, -1) {}
// マージする. すでに同じ集合なら false を返す
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
// alias merge
bool unite(int x, int y) {
return merge(x, y);
}
int root(int x) {
return data[x] < 0 ? x : (data[x] = root(data[x]));
}
vector<int> root_ids() {
vector<int> res;
for (int i = 0; i < data.size(); i++) {
if (data[i] < 0) res.push_back(i);
}
return res;
}
// グループの要素数
int size(int x) { return -data[root(x)]; }
bool issame(int x, int y) {
return root(x) == root(y);
}
};
// #include "../library/tree/SegmentTree.cpp"
// #include "../library/others/Zaatsu.cpp"
/**
* https://ei1333.github.io/luzhiled/snippets/graph/template.html
* グラフのテンプレート
*/
template<typename T>
struct edge {
int src, to;
T cost;
int edge_idx;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template<typename T>
using Edges = vector<edge<T> >;
template<typename T>
using WeightedGraph = vector<Edges<T> >;
using UnWeightedGraph = vector<vector<int> >;
template<typename T>
using Matrix = vector<vector<T> >;
vector<edge<ll>> _we;
template<class T>
WeightedGraph<T> read_weighted_graph(int n, int m, bool directed = false) {
WeightedGraph<T> g(n);
Edges<T> edges;
for (int i = 0; i < m; ++i) {
int a, b;
T v;
*__cin >> a >> b >> v;
a--, b--;
edge e(a, b, v);
e.edge_idx = i;
edges.push_back(e);
g[a].push_back(e);
if (!directed) {
edge rev(b, a, v);
rev.edge_idx = i;
g[b].push_back(rev);
}
}
// _we=edges;
return g;
};
vector<pair<int, int>>_unwaited_edges;
inline UnWeightedGraph read_unwaited_graph(int n, int m, bool directed = false) {
UnWeightedGraph g(n);
_unwaited_edges.clear();
for (int i = 0; i < m; ++i) {
int a, b;
*__cin >> a >> b;
a--, b--;
g[a].push_back(b);
if (!directed)g[b].push_back(a);
_unwaited_edges.push_back({a, b});
}
return g;
}
vector<edge<ll>> _weighted_edges;
inline WeightedGraph<ll> read_1wait_graph(int n, int m, bool directed = false) {
WeightedGraph<ll> g(n);
_weighted_edges.clear();
for (int i = 0; i < m; ++i) {
int a, b;
*__cin >> a >> b;
a--, b--;
edge e(a, b, 1ll);
e.edge_idx = i;
_weighted_edges.push_back(e);
g[a].push_back(e);
if (!directed) {
edge rev(b, a, 1ll);
rev.edge_idx = i;
g[b].push_back(rev);
}
}
return g;
}
WeightedGraph<int> cnv_waited_graph(VVI &g){
WeightedGraph<int> res(g.size());
rep(u,g.size()){
for(auto v:g[u]){
res[u].push_back({u,v,1});
}
}
return res;
}
/**
* weighted graph 上に st からの path が存在するか調べる
* @param g weghted graph
* @param st
* @param reverse 辺を逆向きにする場合は true に
* @return path の存在を {0, 1} のフラグ値で表す vector
*/
template<typename T>
vector<int> has_path_on_wg(WeightedGraph<T> &g, int st, bool reverse = false) {
int n = g.size();
vector<vector<int>> g2(n);
rep(i, n) {
for (auto j: g[i]) {
if (j != i) {
if (reverse) g2[j].push_back(i);
else g2[i].push_back(j);
}
}
}
vector<int> res(g.size(), 0);
res[st] = 1;
queue<int> que;
que.push(st);
while (!que.empty()) {
int u = que.front();
que.pop();
for (auto e: g2[u]) {
if (res[e])continue;
res[e] = 1;
que.push(e);
}
}
return res;
}
// utils
// #include "../library/mint.cpp"
// #include "../library/string/utils.cpp"
// #include "../library/math/utils.cpp"
/**************************/
class yuki {
public:
void solve(std::istream &cin, std::ostream &cout) { _init_io(cin, cout); int t = 1;
// cin >> t;
while (t--) _solve(cin, cout); }
//@formatter:on
void _solve(std::istream &cin, std::ostream &cout) {
// code
int n,m;
cin >> n >> m;
VL in(n,0);
VL out(n,0);
UnionFind uf(n);
rep(i,m){
int a,b;cin>> a>> b;
a--,b--;
in[b]++;
out[a]++;
}
ll totout=0;
ll totin=0;
bool inltout=false;
bool outltin=false;
rep(i,n) {
if(in[i]>out[i]){
if(!inltout){
totin+=(in[i]-1)-out[i];
}else{
totin+=in[i]-out[i];
}
inltout=true;
}
if(in[i]<out[i]){
if(!outltin){
totout+=(out[i]-1)-in[i];
}else{
totout+=out[i]-in[i];
}
outltin=true;
}
}
if(totout==0&&totin==0){
print(0);
}else if(totout!=totin){
print(-1);
}else{
print(totout);
}
}
};
int main() {
yuki solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}