Submission #2894517


Source Code Expand

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = (1e+9)+7;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
bitset<701> exi;
vector<int> G[700],cG[700];
int main() {
	int n, m; cin >> n >> m;
	int a, b;
	rep(i, m) {
		cin >> a >> b; a--; b--;
		G[a].push_back(b); G[b].push_back(a);
	}
	rep(i, n) {
		int used[700] = {};
		int len = G[i].size();
		rep(j, len) {
			used[G[i][j]] = 1;
		}
		rep(j, n) {
			if (i == j)continue;
			if (!used[j]) {
				cG[i].push_back(j);
			}
		}
	}
	int used[700] = {};
	bool g = true;
	queue<P> que;
	rep(i, n) {
		if (used[i])continue;
		used[i] = 1;
		bool chk = true;
		queue<int> q; q.push(i);
		int cnt1 = 0; int cnt2 = 0;
		while (!q.empty()) {
			int len = q.size();
			if (chk)cnt1 += len;
			else cnt2 += len;
			rep(j, len) {
				int nex = q.front(); q.pop();
				int len2 = cG[nex].size();
				rep(k, len2) {
					int v = cG[nex][k];
					if (used[v]) {
						if ((chk&&used[v] == 1) || ((!chk) && used[v] == -1))g = false;
						continue;
					}
					if (chk)used[v] = -1;
					else used[v] = 1;
					q.push(v);
				}
			}
			chk = !chk;
		}
		que.push({ cnt1,cnt2 });
	}
	if (!g) {
		cout << -1 << endl;
	}
	else {
		exi[0] = 1;
		int len = que.size();
		rep(i, len) {
			P nex = que.front(); que.pop();
			int x = nex.first; int y = nex.second;
			exi = (exi << x) | (exi << y);
		}
		int mi = (int)MOD;
		rep(i, n + 1) {
			if (exi[i]) {
				int csum1 = i * (i - 1) / 2;
				int csum2 = (n - i)*(n - i - 1) / 2;
				mi = min(mi, csum1 + csum2);
			}
		}
		cout << mi << endl;
	}
	return 0;
}

Submission Info

Submission Time
Task E - Independence
User heno239
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2421 Byte
Status AC
Exec Time 108 ms
Memory 5248 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 37
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt, sample4.txt
All sample1.txt, sample2.txt, sample3.txt, sample4.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt, sample3.txt, sample4.txt
Case Name Status Exec Time Memory
1.txt AC 3 ms 384 KB
10.txt AC 3 ms 384 KB
11.txt AC 102 ms 3328 KB
12.txt AC 98 ms 3328 KB
13.txt AC 91 ms 3072 KB
14.txt AC 81 ms 3328 KB
15.txt AC 106 ms 3328 KB
16.txt AC 101 ms 3200 KB
17.txt AC 102 ms 3328 KB
18.txt AC 20 ms 896 KB
19.txt AC 107 ms 3328 KB
2.txt AC 19 ms 896 KB
20.txt AC 106 ms 3328 KB
21.txt AC 101 ms 5248 KB
22.txt AC 108 ms 3200 KB
23.txt AC 108 ms 3328 KB
24.txt AC 65 ms 2688 KB
25.txt AC 6 ms 3072 KB
26.txt AC 1 ms 256 KB
27.txt AC 1 ms 256 KB
28.txt AC 1 ms 256 KB
29.txt AC 1 ms 256 KB
3.txt AC 105 ms 3328 KB
4.txt AC 104 ms 3328 KB
5.txt AC 98 ms 3200 KB
6.txt AC 95 ms 3200 KB
7.txt AC 106 ms 3328 KB
8.txt AC 3 ms 384 KB
9.txt AC 20 ms 896 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB
sample3.txt AC 1 ms 256 KB
sample4.txt AC 1 ms 256 KB