提出 #48194665
ソースコード 拡げる
// LUOGU_RID: 138222264
#include<bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#include <vector>
#include <vector>
namespace atcoder {
namespace internal {
template <class T> struct simple_queue {
std::vector<T> payload;
int pos = 0;
void reserve(int n) { payload.reserve(n); }
int size() const { return int(payload.size()) - pos; }
bool empty() const { return pos == int(payload.size()); }
void push(const T& t) { payload.push_back(t); }
T& front() { return payload[pos]; }
void clear() {
payload.clear();
pos = 0;
}
void pop() { pos++; }
};
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class Cap> struct mf_graph {
public:
mf_graph() : _n(0) {}
explicit mf_graph(int n) : _n(n), g(n) {}
int add_edge(int from, int to, Cap cap) {
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
assert(0 <= cap);
int m = int(pos.size());
pos.push_back({from, int(g[from].size())});
int from_id = int(g[from].size());
int to_id = int(g[to].size());
if (from == to) to_id++;
g[from].push_back(_edge{to, to_id, cap});
g[to].push_back(_edge{from, from_id, 0});
return m;
}
struct edge {
int from, to;
Cap cap, flow;
};
edge get_edge(int i) {
int m = int(pos.size());
assert(0 <= i && i < m);
auto _e = g[pos[i].first][pos[i].second];
auto _re = g[_e.to][_e.rev];
return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
}
std::vector<edge> edges() {
int m = int(pos.size());
std::vector<edge> result;
for (int i = 0; i < m; i++) {
result.push_back(get_edge(i));
}
return result;
}
void change_edge(int i, Cap new_cap, Cap new_flow) {
int m = int(pos.size());
assert(0 <= i && i < m);
assert(0 <= new_flow && new_flow <= new_cap);
auto& _e = g[pos[i].first][pos[i].second];
auto& _re = g[_e.to][_e.rev];
_e.cap = new_cap - new_flow;
_re.cap = new_flow;
}
Cap flow(int s, int t) {
return flow(s, t, std::numeric_limits<Cap>::max());
}
Cap flow(int s, int t, Cap flow_limit) {
assert(0 <= s && s < _n);
assert(0 <= t && t < _n);
assert(s != t);
std::vector<int> level(_n), iter(_n);
internal::simple_queue<int> que;
auto bfs = [&]() {
std::fill(level.begin(), level.end(), -1);
level[s] = 0;
que.clear();
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (auto e : g[v]) {
if (e.cap == 0 || level[e.to] >= 0) continue;
level[e.to] = level[v] + 1;
if (e.to == t) return;
que.push(e.to);
}
}
};
auto dfs = [&](auto self, int v, Cap up) {
if (v == s) return up;
Cap res = 0;
int level_v = level[v];
for (int& i = iter[v]; i < int(g[v].size()); i++) {
_edge& e = g[v][i];
if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
Cap d =
self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
if (d <= 0) continue;
g[v][i].cap += d;
g[e.to][e.rev].cap -= d;
res += d;
if (res == up) return res;
}
level[v] = _n;
return res;
};
Cap flow = 0;
while (flow < flow_limit) {
bfs();
if (level[t] == -1) break;
std::fill(iter.begin(), iter.end(), 0);
Cap f = dfs(dfs, t, flow_limit - flow);
if (!f) break;
flow += f;
}
return flow;
}
std::vector<bool> min_cut(int s) {
std::vector<bool> visited(_n);
internal::simple_queue<int> que;
que.push(s);
while (!que.empty()) {
int p = que.front();
que.pop();
visited[p] = true;
for (auto e : g[p]) {
if (e.cap && !visited[e.to]) {
visited[e.to] = true;
que.push(e.to);
}
}
}
return visited;
}
private:
int _n;
struct _edge {
int to, rev;
Cap cap;
};
std::vector<std::pair<int, int>> pos;
std::vector<std::vector<_edge>> g;
};
} // namespace atcoder
using namespace std;
typedef long long ll;
typedef __int128 i128;
#define rep(i,l,r) for(int i(l);i<=(r);++i)
#define per(i,r,l) for(int i(r);i>=(l);--i)
#define eb emplace_back
#define Debug(...) fprintf(stderr,__VA_ARGS__)
int read(){
int ans=0,flg=1;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-')flg=-1;ch=getchar();}
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans*flg;
}
template<typename T>
void read(T &x){
x=0;T flg=1;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-')flg=-1;ch=getchar();}
while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
x*=flg;
}
template<typename T,typename... Args>
void read(T &x,Args &...args){read(x),read(args...);}
void solve(){
int n,m,d,s,t;read(n,d);m=n*d;s=n,t=n+1;
vector<int>deg(n);
vector<pair<int,int>>edge(m);
vector<vector<int>>g(n);
atcoder::mf_graph<i128>G(n+2);
for(auto &[u,v]:edge)read(u,v),u--,v--;
for(auto [u,v]:edge)g[u].eb(v),g[v].eb(u),deg[u]++,deg[v]++;
auto bfs=[&](){
queue<int>q;vector<int>vis(n,0);
q.push(0);vis[0]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(auto v:g[u])if(!vis[v])q.push(v),vis[v]=1;
}
return find(vis.begin(),vis.end(),0)==vis.end();
};
if(!bfs())return puts("No"),void();
const ll inf=1e13;
for(auto [u,v]:edge)G.add_edge(u,v,inf-1),G.add_edge(v,u,inf-1);
for(int i=0;i<n;i++)G.add_edge(s,i,inf*m),G.add_edge(i,t,inf*(m+2*d-deg[i]));
auto res=G.flow(s,t);
puts((i128)n*m*inf>res?"No":"Yes");
}
int main(){
int T;read(T);
while(T--)solve();
return 0;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
after_contest |
| 得点 / 配点 |
0 / 0 |
900 / 900 |
0 / 0 |
| 結果 |
|
|
|
| セット名 |
テストケース |
| Sample |
00-sample-001.txt |
| All |
00-sample-001.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt |
| after_contest |
after_contest_001.txt, after_contest_002.txt, after_contest_003.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00-sample-001.txt |
AC |
1 ms |
3492 KiB |
| 01-001.txt |
AC |
12 ms |
3592 KiB |
| 01-002.txt |
AC |
12 ms |
3744 KiB |
| 01-003.txt |
AC |
11 ms |
4376 KiB |
| 01-004.txt |
AC |
9 ms |
4312 KiB |
| 01-005.txt |
AC |
8 ms |
3996 KiB |
| 01-006.txt |
AC |
23 ms |
4420 KiB |
| 01-007.txt |
AC |
20 ms |
8816 KiB |
| 01-008.txt |
AC |
19 ms |
8804 KiB |
| 01-009.txt |
AC |
12 ms |
4240 KiB |
| 01-010.txt |
AC |
10 ms |
4696 KiB |
| 01-011.txt |
AC |
9 ms |
8372 KiB |
| 01-012.txt |
AC |
10 ms |
8380 KiB |
| 01-013.txt |
AC |
23 ms |
26784 KiB |
| 01-014.txt |
AC |
26 ms |
22612 KiB |
| 01-015.txt |
AC |
20 ms |
18212 KiB |
| 01-016.txt |
AC |
15 ms |
17100 KiB |
| 01-017.txt |
AC |
18 ms |
9852 KiB |
| 01-018.txt |
AC |
18 ms |
9776 KiB |
| 01-019.txt |
AC |
12 ms |
8400 KiB |
| 01-020.txt |
AC |
12 ms |
8416 KiB |
| 01-021.txt |
AC |
15 ms |
6316 KiB |
| 01-022.txt |
AC |
12 ms |
14132 KiB |
| 01-023.txt |
AC |
12 ms |
14312 KiB |
| 01-024.txt |
AC |
12 ms |
7232 KiB |
| 01-025.txt |
AC |
12 ms |
7324 KiB |
| 01-026.txt |
AC |
14 ms |
5112 KiB |
| 01-027.txt |
AC |
7 ms |
3740 KiB |
| 01-028.txt |
AC |
6 ms |
3676 KiB |
| 01-029.txt |
AC |
13 ms |
3496 KiB |
| 01-030.txt |
AC |
44 ms |
27484 KiB |
| 01-031.txt |
AC |
47 ms |
27604 KiB |
| 01-032.txt |
AC |
75 ms |
26396 KiB |
| 01-033.txt |
AC |
29 ms |
22932 KiB |
| 01-034.txt |
AC |
37 ms |
22816 KiB |
| 01-035.txt |
AC |
87 ms |
20560 KiB |
| 01-036.txt |
AC |
19 ms |
18032 KiB |
| 01-037.txt |
AC |
20 ms |
18108 KiB |
| 01-038.txt |
AC |
35 ms |
17316 KiB |
| 01-039.txt |
AC |
13 ms |
5784 KiB |
| 01-040.txt |
AC |
13 ms |
3744 KiB |
| 01-041.txt |
AC |
341 ms |
27584 KiB |
| 01-042.txt |
AC |
334 ms |
27548 KiB |
| after_contest_001.txt |
AC |
15 ms |
4596 KiB |
| after_contest_002.txt |
AC |
24 ms |
20688 KiB |
| after_contest_003.txt |
AC |
25 ms |
20748 KiB |