QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#618825#7898. I Just Want... One More...AmiyaCastWA 11ms3612kbC++202.3kb2024-10-07 10:43:012024-10-07 10:43:01

Judging History

你现在查看的是最新测评结果

  • [2024-10-07 10:43:01]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:3612kb
  • [2024-10-07 10:43:01]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int, int>
const ll inf = 1145141919810;
using namespace std;
struct Dinic {
	ll st, ed, n, m, cnt;
	vector<ll> nxt{0, 0}, to{0, 0}, c{0, 0}, head, pre, cur, d;

	Dinic(int _n, int _st, int _ed){
		n = _n, st = _st, ed = _ed, cnt = 1;
		head.resize(n + 10), pre.resize(n + 10), cur.resize(n + 10), d.resize(n + 10);
	}
    
    void Add(int x, int y, ll z) { nxt.push_back(head[x]), to.push_back(y), c.push_back(z), head[x] = ++cnt;};
	void add(int x, int y, ll z) { Add(x, y, z), Add(y, x, 0);}
	bool bfs() {
		for(int i = 1; i <= n; ++i) d[i] = 0;
		queue<ll> q;
		q.push(st), d[st] = 1;
		while(q.size()) {
			int x = q.front();
			q.pop();
			for(int i = head[x]; i; i = nxt[i])
				if(d[to[i]] == 0 && c[i]) {
					d[to[i]] = d[x] + 1;
					q.push(to[i]);
					if(to[i] == ed) return 1;
				}
		}
		return 0;
	}
	ll dfs(int x, ll mf) {
		if(x == ed) return mf;
		ll sum = 0;
		for(int i = cur[x]; i; i = nxt[i]) {
			cur[x] = i;
			if(d[to[i]] == d[x] + 1 && c[i]) {
				ll f = dfs(to[i], min(c[i], mf));
				c[i] -= f, c[i ^ 1] += f;
				sum += f, mf -= f;
				if(mf == 0) break;
			}
		}
		if(sum == 0) d[x] = 0;//剪枝
		return sum;//这个就是这个点的ans
	}
	ll dinic() {
		ll flow = 0;
		while(bfs()) cur = head, flow += dfs(st, inf);
		return flow;
	}
};
void slv(){
    int n, m;
    cin >> n >> m;
    Dinic d(2 * n, 2 * n + 1, 2 * n + 2);
	vector<int> v1, v2, v(2 * n + 5);
	queue<int> st, ed;

	for(int i = 1; i <= n; ++i) d.add(d.st, i, 1);
	for(int i = 1; i <= n; ++i) d.add(i + n, d.ed, 1);

	while(m--){
		int x, y;
		cin >> x >> y;
		d.add(x, y + n, 1);
	}

	d.dinic();
	st.push(d.st), ed.push(d.ed);

	while(st.size()){
		int x = st.front(); st.pop();
		v[x] = 1;
		if(x != d.st && x <= n) v1.push_back(x);
		for(int i = d.head[x]; i; i = d.nxt[i]){
			if(d.c[i] && !v[d.to[i]]) st.push(d.to[i]);
		}
	}
	while(ed.size()){
		int x = ed.front(); ed.pop();
		v[x] = 1;
		if(x != d.ed && x > n) v2.push_back(x);
		for(int i = d.head[x]; i; i = d.nxt[i]){
			if(d.c[i ^ 1] && !v[d.to[i]]) ed.push(d.to[i]);
		}
	}

	cout << (ll)v1.size() * v2.size() << endl;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int _ = 1;
    cin >> _;
    while(_--) slv();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

3
4 3
1 2
3 2
4 3
3 3
1 3
2 2
3 1
3 2
1 2
1 2

output:

6
0
4

result:

ok 3 number(s): "6 0 4"

Test #2:

score: -100
Wrong Answer
time: 11ms
memory: 3504kb

input:

10000
5 4
2 2
3 1
5 3
1 1
1 1
1 1
1 1
1 1
2 2
2 2
1 2
1 1
1 1
1 1
1 1
1 1
1 1
2 3
2 1
1 2
1 1
5 5
1 4
2 2
3 1
1 3
1 2
2 4
2 2
2 1
1 2
1 1
5 1
5 3
3 1
2 2
1 1
1 1
3 2
3 1
2 1
5 2
1 2
2 3
5 3
1 5
4 2
1 2
4 1
1 1
2 3
1 1
2 2
2 1
4 1
1 4
3 1
1 1
1 1
1 1
2 1
2 2
3 3
1 3
2 3
2 2
3 3
1 3
3 3
1 2
3 3
2 2
1 ...

output:

6
0
0
2
0
0
0
0
6
0
16
4
0
6
9
9
9
0
9
4
0
1
1
1
0
4
16
12
3
2
16
0
2
2
24
1
0
0
0
0
16
4
4
16
4
9
0
9
0
2
3
0
9
4
9
16
20
0
0
1
12
0
1
2
0
0
1
0
0
2
2
4
0
12
1
0
0
2
1
2
2
3
0
4
1
6
0
0
0
0
9
16
2
0
1
2
0
12
2
4
0
12
4
1
9
4
6
9
9
12
3
16
15
16
9
4
9
0
1
16
9
9
1
9
16
9
12
4
9
2
0
4
0
6
0
3
0
0
0
0...

result:

wrong answer 35th numbers differ - expected: '20', found: '24'