QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#688780 | #1957. Friendship Graphs | k1nsom# | WA | 131ms | 11628kb | C++17 | 1.9kb | 2024-10-30 13:31:39 | 2024-10-30 13:31:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define PII pair<int, int>
const int N = 1e3 + 10;
int n, m, dp[2][N], vis[N];
vector<int> e[N];
vector<int> tmp, g[N];
void dfs(int u)
{
tmp.push_back(u);
for (auto to : g[u])
if (!vis[to])
vis[to] = 3 ^ vis[u], dfs(to);
else if (vis[to] == vis[u])
{
cout << -1 << endl;
exit(0);
}
}
int top = 0;
PII stk[N];
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
e[i].resize(n + 1), fill(e[i].begin(), e[i].end(), 1);
for (int i = 1; i <= n; i++)
e[i][i] = 0;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
e[u][v] = 0, e[v][u] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (e[i][j])
g[i].push_back(j);
}
}
for (int i = 1; i <= n; i++)
if (!vis[i])
{
vis[i] = 1;
tmp.clear();
dfs(i);
int cnt = 0;
for (auto to : tmp)
if (vis[to] == 1)
cnt++;
stk[++top] = {cnt, tmp.size() - cnt};
}
dp[0][0] = 1;
for (int i = 1; i <= top; i++)
{
for (int j = n; j >= stk[i].first; j--)
dp[i & 1][j] |= dp[1 ^ (i & 1)][j - stk[i].first];
for (int j = n; j >= stk[i].second; j--)
dp[i & 1][j] |= dp[1 ^ (i & 1)][j - stk[i].second];
}
int ans = n;
for (int i = 1; i <= n / 2; i++)
if (dp[n & 1][i])
ans = min(ans, n - i - i);
cout << ans << endl;
}
signed main()
{
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 124ms
memory: 11628kb
input:
1000 499000 20 615 260 390 779 37 13 563 650 605 358 684 783 370 162 90 379 662 88 208 458 371 178 3 590 762 455 514 738 641 270 805 205 881 205 315 837 54 976 579 519 532 982 669 563 804 648 274 268 293 182 392 337 772 961 603 294 607 546 772 189 218 702 266 515 610 691 965 643 235 509 193 184 302 ...
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 131ms
memory: 11628kb
input:
1000 498999 35 65 880 835 501 309 590 758 882 857 356 493 43 623 644 637 361 785 58 317 26 11 595 521 723 629 611 36 789 29 30 650 426 475 852 862 667 137 677 173 656 44 457 279 680 567 789 989 368 873 510 721 128 584 835 956 419 779 607 568 317 790 932 580 336 400 74 265 772 855 939 816 448 883 381...
output:
0
result:
wrong answer 1st lines differ - expected: '2', found: '0'