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

Compiler report

input/code.cpp: In function 'int find_min()':
input/code.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (g[i].size() < mcnt) {
       ~~~~~~~~~~~~^~~~~~
input/code.cpp:83:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (g[i].size() == mcnt) {
       ~~~~~~~~~~~~^~~~~~~

Code

#include <iostream>
#include <vector>
#include <set>

using namespace std;
typedef long double ld;
#define F first
#define S second

int n, m;
set<int> g[105];
bool rm[105];

int cmp[105];
int cmpv[105];
int cmpe[105];

ld mxrt = 0.0;
int resn = 0;
int resm = 0;
vector<int> resnd;

pair<int, int> density() {
	int vc = 0;
	int ec = 0;
	for (int i = 1; i <= n; ++i) {
		if (rm[i]) continue;
		vc++;
		ec += g[i].size();
	}
	ec /= 2;
	return {vc, ec};
}

void dfs(int s, int cc) {
	if (cmp[s] != 0) return;
	cmp[s] = cc;
	cmpv[cc]++;
	cmpe[cc] += g[s].size();

	for (int a : g[s]) {
		dfs(a, cc);
	}
}

void ccmpd() {
	for (int i = 1; i <= n; ++i) {
		cmp[i] = 0;
		cmpe[i] = 0;
		cmpv[i] = 0;
	}
	int ccmp = 0;
	for (int i = 1; i <= n; ++i) {
		if (rm[i]) continue;
		if (cmp[i] == 0) {
			ccmp++;
			dfs(i, ccmp);
		}
	}

	for (int i = 1; i <= n; ++i) {
		cmpe[i] /= 2;
	}
}

int find_min() {
	int mcnt = 1000000000;
	int mind = -1;
	ld bres = 1000000000.0;
	for (int i = 1; i <= n; ++i) {
		if (rm[i]) continue;
		if (g[i].size() < mcnt) {
			mcnt = g[i].size();
			mind = i;
			int necnt = cmpe[cmp[i]]; // -g[i].size();
			int nvcnt = cmpv[cmp[i]]; // -1;
			ld cres = 0.0;
			if (nvcnt > 0) {
				cres = necnt / ((ld)nvcnt);
			}
			bres = cres;
		}
		if (g[i].size() == mcnt) {
			int necnt = cmpe[cmp[i]]; // -g[i].size();
			int nvcnt = cmpv[cmp[i]]; // -1;
			ld cres = 0.0;
			if (nvcnt > 0) {
				cres = necnt / ((ld)nvcnt);
			}
			if (cres <= bres) {
				bres = cres;
				mcnt = g[i].size();
				mind = i;
			}
		}
	}
	return mind;
}

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].insert(b);
		g[b].insert(a);
	}
	int gsz = n;
	while (gsz > 0) {
		auto cden = density();
		ld crt = cden.S / ((ld)cden.F);
		if (crt > mxrt) {
			mxrt = crt;
			resn = cden.F;
			resm = cden.S;
			resnd.clear();
			for (int i = 1; i <= n; ++i) {
				if (rm[i]) continue;
				resnd.push_back(i);
			}
		}

		ccmpd();

		int cmin = find_min();
		//cout << "remove " << cmin << endl;
		rm[cmin] = true;
		for (int a : g[cmin]) {
			g[a].erase(cmin);
		}
		gsz--;
	}
	cout << resn << " " << resm << "\n";
	for (int a : resnd) {
		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
2 3 4 5 6 7 8 

Test 2

Verdict:

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
39 51
3 6 7 8 9 10 12 15 20 21 23 24...
Truncated

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
2 3 4 6 7 9 10 11 14 15 16 18 ...
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
1 2 3 5 6 7 8 9 10 11 12 13 14...
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
1 2 3 4 5 6 8 9 10 11 12 13 14...
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
1 2 4 5 6 7 8 9 10 11 12 13 14...
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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
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 4 5 7 8 10 12 15 16 19 

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
1 4 11 12 13 14 15 19 20 

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
2 3 4 5 6 8 9 10 11 12 13 14 1...

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

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
1 2 3 4 5 6 7 8 9 10 

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
2 4 6 7 8 9 10 

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
12 13 14 15 19 

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 9 10 11 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
8 9 10 11 12 13 14