Submission #2736564
Source Code Expand
/** * File : E.cpp * Author : Kazune Takahashi * Created : 2018-6-23 21:49:57 * Powered by Visual Studio Code */ #include <iostream> #include <iomanip> // << fixed << setprecision(xxx) #include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ; #include <vector> #include <string> // to_string(nnn) // substr(m, n) // stoi(nnn) #include <complex> #include <tuple> #include <queue> #include <stack> #include <map> // if (M.find(key) != M.end()) { } #include <set> #include <random> // random_device rd; mt19937 mt(rd()); #include <chrono> // std::chrono::system_clock::time_point start_time, end_time; // start = std::chrono::system_clock::now(); // double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count(); #include <cctype> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> using namespace std; #define DEBUG 0 // change 0 -> 1 if we need debug. typedef long long ll; // const int dx[4] = {1, 0, -1, 0}; // const int dy[4] = {0, 1, 0, -1}; // const int C = 1e6+10; // const ll M = 1000000007; int N, M; set<int> W[1000]; vector<int> V[1000]; vector<pair<int, int>> res; int visited[1000]; int cnt[2]; bool nuri(int v, int k) { visited[v] = k; for (auto x : V[v]) { if (visited[x] == 0) { if (!nuri(x, 3 - k)) { return false; } } else if (visited[x] == k) { return false; } } cnt[k - 1]++; return true; } bool reach[1000][1000]; int main() { cin >> N >> M; for (auto i = 0; i < M; i++) { int a, b; cin >> a >> b; a--; b--; W[a].insert(b); W[b].insert(a); } for (auto i = 0; i < N; i++) { for (auto j = 0; j < N; j++) { if (i == j) continue; if (W[i].find(j) == W[i].end()) { V[i].push_back(j); // cerr << "V[" << i << "].push_back(" << j << ")" << endl; } } } fill(visited, visited + 1000, 0); for (auto k = 0; k < N; k++) { if (visited[k] != 0) { continue; } cnt[0] = cnt[1] = 0; if (!nuri(k, 1)) { cout << -1 << endl; return 0; } res.push_back(make_pair(cnt[0], cnt[1])); } fill(&reach[0][0], &reach[0][0] + 1000 * 1000, false); reach[0][0] = true; for (int i = 0; i < (int)res.size(); i++) { auto e = res[i]; int x = e.first; int y = e.second; // cerr << "i = " << i << endl; // cerr << "x = " << x << ", y = " << y << endl; for (auto j = N; j >= 0; j--) { if (reach[i][j]) { // cerr << "j = " << j << endl; reach[i + 1][x + j] = true; reach[i + 1][y + j] = true; } } } int ans = 1000000007; for (auto i = 0; i <= N; i++) { int j = N - i; if (reach[(int)res.size()][i]) { ans = min(ans, i * (i - 1) / 2 + j * (j - 1) / 2); } } cout << ans << endl; }
Submission Info
Submission Time | |
---|---|
Task | E - Independence |
User | kazunetakahashi |
Language | C++14 (GCC 5.4.1) |
Score | 700 |
Code Size | 3032 Byte |
Status | AC |
Exec Time | 360 ms |
Memory | 24192 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 700 / 700 | ||||
Status |
|
|
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 | 5 ms | 1664 KB |
10.txt | AC | 5 ms | 768 KB |
11.txt | AC | 354 ms | 23168 KB |
12.txt | AC | 291 ms | 22272 KB |
13.txt | AC | 282 ms | 20864 KB |
14.txt | AC | 245 ms | 17920 KB |
15.txt | AC | 335 ms | 23168 KB |
16.txt | AC | 346 ms | 21632 KB |
17.txt | AC | 327 ms | 23040 KB |
18.txt | AC | 52 ms | 5376 KB |
19.txt | AC | 360 ms | 24192 KB |
2.txt | AC | 49 ms | 5248 KB |
20.txt | AC | 351 ms | 24064 KB |
21.txt | AC | 320 ms | 22784 KB |
22.txt | AC | 323 ms | 23168 KB |
23.txt | AC | 344 ms | 24192 KB |
24.txt | AC | 189 ms | 15104 KB |
25.txt | AC | 5 ms | 3200 KB |
26.txt | AC | 2 ms | 1280 KB |
27.txt | AC | 2 ms | 1280 KB |
28.txt | AC | 2 ms | 1280 KB |
29.txt | AC | 1 ms | 256 KB |
3.txt | AC | 336 ms | 22912 KB |
4.txt | AC | 329 ms | 23552 KB |
5.txt | AC | 306 ms | 21248 KB |
6.txt | AC | 309 ms | 21504 KB |
7.txt | AC | 340 ms | 24064 KB |
8.txt | AC | 6 ms | 1792 KB |
9.txt | AC | 50 ms | 5376 KB |
sample1.txt | AC | 2 ms | 1280 KB |
sample2.txt | AC | 1 ms | 256 KB |
sample3.txt | AC | 2 ms | 1280 KB |
sample4.txt | AC | 2 ms | 1280 KB |