Submission details
Task:Densest subgraph
Sender:Pohjantahti
Submission time:2018-09-18 18:22:48 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.03 sdetails
#3ACCEPTED0.07 sdetails
#4ACCEPTED0.12 sdetails
#5ACCEPTED0.16 sdetails
#6ACCEPTED0.19 sdetails
#7ACCEPTED0.02 sdetails
#8ACCEPTED0.02 sdetails
#9ACCEPTED0.04 sdetails
#10ACCEPTED0.04 sdetails
#11ACCEPTED0.03 sdetails
#12ACCEPTED0.02 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.02 sdetails
#15ACCEPTED0.02 sdetails
#16ACCEPTED0.02 sdetails
#17ACCEPTED0.03 sdetails
#18ACCEPTED0.03 sdetails
#19ACCEPTED0.01 sdetails
#20ACCEPTED0.02 sdetails
#21ACCEPTED0.03 sdetails
#22ACCEPTED0.02 sdetails
#23ACCEPTED0.03 sdetails
#24ACCEPTED0.01 sdetails

Compiler report

input/code.cpp: In function 'bool flow_ok(ld)':
input/code.cpp:80:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < cp.size()-1; ++i) {
                    ~~^~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 100;
const int S = 0;
const int T = N+1;

int n, m;
vector<int> g[N+5];
ll d[N+5][N+5];
int v[N+5];
int gvis = 0;

vector<int> cp;

bool rvis[N+5];
vector<int> res;

void set_weights(ld gp) {
	const ll MP = 1000000000;
	ll ig = (ll)(gp*MP);

	for (int a : g[S]) {
		d[S][a] = m*MP;
		d[a][S] = 0;
	}
	for (int a : g[T]) {
		d[a][T] = m*MP + 2*ig - (g[a].size()-2)*MP;
		d[T][a] = 0;
	}
	for (int i = 1; i <= n; ++i) {
		for (int a : g[i]) {
			if (a == S || a == T) continue;
			d[i][a] = 1*MP;
		}
	}
}

ll dfs(int s, int t, ll th, int cvis, ll cmin) {
	if (v[s] == cvis) return -1;
	v[s] = cvis;
	cp.push_back(s);
	if (s == t) return cmin;

	for (int a : g[s]) {
		if (d[s][a] < th) continue;
		ll cres = dfs(a, t, th, cvis, min(cmin, d[s][a]));
		if (cres != -1) return cres;
	}
	cp.pop_back();
	return -1;
}

void get_res(int s) {
	if (rvis[s]) return;
	rvis[s] = true;
	if (!(s == S || s == T)) {
		res.push_back(s);
	}
	for (int a : g[s]) {
		if (d[s][a] == 0) continue;
		get_res(a);
	}
}

bool flow_ok(ld gp) {
	set_weights(gp);

	ll cth = (1LL<<60);
	while (true) {
		gvis++;
		cp.clear();
		ll minw = dfs(S, T, cth, gvis, 1000000000000000005);
		if (minw != -1) {
			for (int i = 0; i < cp.size()-1; ++i) {
				int fn = cp[i];
				int sn = cp[i+1];
				d[fn][sn] -= minw;
				d[sn][fn] += minw;
			}
		}
		else {
			if (cth == 1) break;
			cth /= 2;
		}
	}

	for (int a : g[S]) {
		if (d[S][a] > 0) {
			return true;
		}
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for (int i = 1; i <= n; ++i) {
		g[S].push_back(i);
		g[i].push_back(S);
		g[i].push_back(T);
		g[T].push_back(i);
	}

	ld gp = 0;
	ld jp = (1LL<<30);
	for (int i = 0; i < 100; ++i) {
		if (flow_ok(gp+jp)) gp += jp;
		jp /= 2;
	}
	flow_ok(gp); // build densest subgraph
	get_res(S); // find densest subgraph

	int rn = 0, rm = 0;
	for (int i = 1; i <= n; ++i) {
		if (rvis[i]) {
			rn++;
			for (int a : g[i]) {
				if (!rvis[a] || a == S || a == T) continue;
				rm++;
			}
		}
	}
	rm /= 2;
	cout << rn << " " << rm << "\n";
	for (int a : res) {
		cout << a << " ";
	}
	cout << "\n";
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
8 11
1 2
2 3
3 4
4 5
...

correct output
7 10
2 3 4 5 6 7 8 

user output
7 10
5 4 3 2 8 6 7 

Test 2

Verdict: ACCEPTED

input
100 100
1 97
2 14
2 81
3 34
...

correct output
28 37
3 6 7 9 10 12 15 21 23 27 28 3...

user output
28 37
47 23 54 21 94 7 6 92 28 15 98...

Test 3

Verdict: ACCEPTED

input
100 200
1 99
2 11
2 28
2 59
...

correct output
71 156
2 3 4 6 7 9 10 11 14 15 16 18 ...

user output
71 156
87 16 14 4 20 15 9 28 2 11 21 ...
Truncated

Test 4

Verdict: ACCEPTED

input
100 300
1 24
1 38
1 65
1 83
...

correct output
93 284
1 2 3 5 6 7 8 9 10 11 12 13 14...

user output
93 284
100 52 29 18 20 30 12 6 16 9 5...
Truncated

Test 5

Verdict: ACCEPTED

input
100 400
1 44
1 56
1 77
1 87
...

correct output
91 368
1 2 3 4 5 6 8 9 10 11 12 13 14...

user output
91 368
95 28 10 33 4 2 15 3 26 18 20 ...
Truncated

Test 6

Verdict: ACCEPTED

input
100 500
1 19
1 40
1 51
1 66
...

correct output
92 465
1 2 4 5 6 7 8 9 10 11 12 13 14...

user output
92 465
96 26 6 8 24 16 11 20 10 4 2 7...
Truncated

Test 7

Verdict: ACCEPTED

input
32 496
1 2
1 3
1 4
1 5
...

correct output
32 496
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
32 496
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 8

Verdict: ACCEPTED

input
44 462
1 2
1 3
1 4
1 5
...

correct output
44 462
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
44 462
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 9

Verdict: ACCEPTED

input
56 495
1 2
1 3
1 4
1 5
...

correct output
38 342
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
38 342
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 10

Verdict: ACCEPTED

input
32 495
1 2
1 3
1 4
1 5
...

correct output
32 495
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
32 495
16 1 3 2 4 6 5 7 9 8 10 12 11 ...
Truncated

Test 11

Verdict: ACCEPTED

input
44 461
1 2
1 3
1 4
1 5
...

correct output
22 231
23 24 25 26 27 28 29 30 31 32 ...

user output
22 231
23 24 25 26 27 28 29 30 31 32 ...

Test 12

Verdict: ACCEPTED

input
56 494
1 2
1 3
1 4
1 5
...

correct output
19 171
20 21 22 23 24 25 26 27 28 29 ...

user output
19 171
20 21 22 23 24 25 26 27 28 29 ...

Test 13

Verdict: ACCEPTED

input
20 1
5 18

correct output
2 1
5 18 

user output
2 1
5 18 

Test 14

Verdict: ACCEPTED

input
20 10
2 15
2 16
4 16
5 10
...

correct output
10 9
2 4 5 7 8 10 12 15 16 19 

user output
10 9
2 15 8 19 12 16 4 5 10 7 

Test 15

Verdict: ACCEPTED

input
20 20
1 13
1 14
1 15
1 19
...

correct output
9 13
1 4 11 12 13 14 15 19 20 

user output
9 13
20 12 4 11 14 1 13 19 15 

Test 16

Verdict: ACCEPTED

input
20 50
1 3
1 19
2 13
2 14
...

correct output
17 44
2 3 4 5 6 8 9 10 11 12 13 14 1...

user output
17 44
20 2 14 3 4 8 5 10 19 11 9 16 ...

Test 17

Verdict: ACCEPTED

input
20 100
1 2
1 6
1 7
1 8
...

correct output
19 96
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
19 96
19 1 2 3 7 5 6 10 9 11 8 4 12 ...

Test 18

Verdict: ACCEPTED

input
20 190
1 2
1 3
1 4
1 5
...

correct output
20 190
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
20 190
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 19

Verdict: ACCEPTED

input
20 90
1 2
1 3
1 4
1 5
...

correct output
10 45
1 2 3 4 5 6 7 8 9 10 

user output
20 90
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 20

Verdict: ACCEPTED

input
20 50
1 2
1 4
1 6
1 7
...

correct output
10 28
1 2 3 4 5 6 7 8 9 10 

user output
10 28
2 1 4 3 5 6 8 9 10 7 

Test 21

Verdict: ACCEPTED

input
20 20
1 9
2 6
2 7
2 9
...

correct output
7 10
2 4 6 7 8 9 10 

user output
7 10
10 4 8 7 2 6 9 

Test 22

Verdict: ACCEPTED

input
20 10
2 7
4 10
5 7
12 19
...

correct output
5 4
12 13 14 15 19 

user output
5 4
19 12 13 14 15 

Test 23

Verdict: ACCEPTED

input
20 10
8 11
8 13
9 11
10 11
...

correct output
3 3
10 11 12 

user output
6 6
8 11 9 10 12 13 

Test 24

Verdict: ACCEPTED

input
20 50
1 2
1 4
1 5
2 3
...

correct output
7 20
8 9 10 11 12 13 14 

user output
7 20
14 8 10 9 11 12 13