QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#615796 | #9449. New School Term | ucup-team3670# | WA | 1ms | 3792kb | C++17 | 3.5kb | 2024-10-05 20:12:07 | 2024-10-05 20:12:10 |
Judging History
answer
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
using namespace std;
const int MOD = 981712597;
int add(int a, int b){
a += b;
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
return a;
}
struct edge{
int v, u;
};
vector<int> rk, p, clr, siz;
int getp(int a){
vector<int> path;
while (a != p[a]){
path.push_back(a);
a = p[a];
}
reverse(path.begin(), path.end());
forn(i, int(path.size()) - 1){
clr[path[i + 1]] ^= clr[path[i]];
p[path[i + 1]] = a;
}
return a;
}
bool check(int a, int b){
int pa = getp(a), pb = getp(b);
if (pa != pb) return true;
return clr[a] != clr[b];
}
void unite(int a, int b){
assert(check(a, b));
int pa = getp(a), pb = getp(b);
if (pa == pb) return;
if (rk[pa] < rk[pb]) swap(pa, pb);
p[pb] = pa;
rk[pa] += rk[pb];
clr[pb] = clr[a] ^ clr[b] ^ 1;
siz[pa] += clr[pb] == 0 ? siz[pb] : rk[pb] - siz[pb];
}
vector<int> dp;
void add(int x){
if (x == 0) return;
for (int j = int(dp.size()) - 1; j >= 0; --j) if (j + x < int(dp.size()))
dp[j + x] = add(dp[j + x], dp[j]);
}
void rem(int x){
if (x == 0) return;
forn(j, int(dp.size())) if (j + x < int(dp.size()))
dp[j + x] = add(dp[j + x], -dp[j]);
}
int getx(int sum, int x){
if (sum < 0) return 0;
if (sum < x || x == 0) return dp[sum];
return add(dp[sum], -dp[sum - x]);
}
int getxy(int sum, int x, int y){
if (sum < 0) return 0;
if (y == 0) swap(x, y);
if (y == 0) return dp[sum];
int cntx = getx(sum, x);
int cnty = add(cntx, -getx(sum - y, x));
return cnty;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<edge> e(m);
forn(i, m){
cin >> e[i].v >> e[i].u;
--e[i].v, --e[i].u;
}
rk.assign(2 * n, 1);
p.resize(2 * n);
iota(p.begin(), p.end(), 0);
clr.assign(2 * n, 0);
siz.assign(2 * n, 1);
dp.resize(n + 1);
dp[0] = 1;
forn(i, 2 * n) add(1);
int sum = 0;
for (int i = m - 1; i >= 0; --i){
int v = e[i].v, u = e[i].u;
if (!check(v, u)) continue;
int pv = getp(v), pu = getp(u);
if (pv == pu) continue;
int mnv = min(siz[pv], rk[pv] - siz[pv]);
int mnu = min(siz[pu], rk[pu] - siz[pu]);
int x = rk[pv] - 2 * mnv;
int y = rk[pu] - 2 * mnu;
int nsum = sum;
nsum -= mnv + mnu;
int one = (clr[v] ^ clr[u] ^ 1) == 0 ? siz[pv] + siz[pu] : siz[pv] + (rk[pu] - siz[pu]);
one = min(one, rk[pv] + rk[pu] - one);
nsum += one;
int xy = (rk[pv] + rk[pu] - one) - one;
if (getxy(n - nsum, x, y) == 0 && getxy(n - nsum - xy, x, y) == 0) continue;
rem(x); rem(y);
unite(v, u);
sum = nsum;
add(xy);
}
vector<int> lst(n + 1);
dp.assign(n + 1, 0);
dp[0] = 1;
sum = 0;
forn(i, 2 * n) if (i == getp(i)){
int mn = min(siz[i], rk[i] - siz[i]);
sum += mn;
int x = rk[i] - 2 * mn;
for (int j = n - x; j >= 0; --j) if (dp[j] && !dp[j + x]){
dp[j + x] = true;
lst[j + x] = i;
}
}
int need = n - sum;
vector<char> big(2 * n);
while (need > 0){
int i = lst[need];
int mn = min(siz[i], rk[i] - siz[i]);
int x = rk[i] - 2 * mn;
big[i] = true;
need -= x;
}
vector<vector<vector<int>>> comp(2 * n, vector<vector<int>>(2));
forn(i, 2 * n){
int v = getp(i);
int c = siz[v] < rk[v] - siz[v] ? 0 : 1;
comp[v][clr[i] ^ c].push_back(i);
}
string res(2 * n, '0');
forn(i, 2 * n) if (i == getp(i)){
for (int v : comp[i][big[i]]){
res[v] = '1';
}
}
cout << res << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3612kb
input:
2 4 1 3 2 4 1 4 1 2
output:
0101
result:
ok Output is valid. OK
Test #2:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
3 7 2 5 1 3 4 6 2 6 4 5 2 4 5 6
output:
001101
result:
ok Output is valid. OK
Test #3:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
1 0
output:
10
result:
ok Output is valid. OK
Test #4:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
1 1 1 2
output:
01
result:
ok Output is valid. OK
Test #5:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
2 3 2 4 3 4 1 2
output:
0110
result:
ok Output is valid. OK
Test #6:
score: 0
Accepted
time: 1ms
memory: 3792kb
input:
3 8 4 6 3 5 1 4 2 4 1 6 1 2 3 4 4 5
output:
010101
result:
ok Output is valid. OK
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3532kb
input:
4 9 4 7 3 8 1 5 2 7 2 8 6 8 7 8 1 4 1 6
output:
01111110
result:
wrong answer The number of 0s must be equal to that of 1s.