QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#621163 | #9449. New School Term | APROHACK124 | RE | 0ms | 0kb | C++14 | 4.1kb | 2024-10-08 10:21:31 | 2024-10-08 10:22:08 |
answer
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
using ll = long long;
using ld = long double;
using namespace std;
int n, m;
vector<pair<int, int > > friends;
struct node{
int rep;
int color;
vector<int>group;
int cnt_color[2];
void append(vector<int>&g){
for(auto &i : g){
group.pb(i);
}
}
};
vector<pair<int, int> > exec;
int sq = 140;
node nodes[10004];
int findd(int u ){
if(nodes[u].rep == u)return u;
return nodes[u].rep = findd(nodes[u].rep);
}
int joinn(int a, int b){
a = findd(a);
b = findd(b);
if(a == b)return a;
if(nodes[a].group.size() < nodes[b].group.size()){
swap(a, b);
}
// a <- b
node &A = nodes[a];
node &B = nodes[b];
A.cnt_color[0] += B.cnt_color[0];
A.cnt_color[1] += B.cnt_color[1];
for(auto &i : B.group){
A.group.pb(i);
}
B.group.clear();
B.rep = A.rep;
return B.rep;
}
void solve(set<int>&important){
if(exec.empty())return ;
set<int>others;
vector<int>allOthers;
for(int i = 1 ; i <= 2 * n ; i ++){
int rep = findd(rep);
if(important.find(rep) != important.end())continue;
others.insert(rep);
}
for(auto i : others)allOthers.pb(i);
bitset<5005>bitmask;
auto getBitmask = [&] (){
for(int i = 0 ; i <= n + 1 ; i ++){
bitmask[i] = 0;
}
bitmask[0] = 1;
for(int i = 0 ; i < allOthers.size() ; i ++){
int node = findd(allOthers[i]);
bitmask = ((bitmask << nodes[node].cnt_color[0]) |(bitmask << nodes[node].cnt_color[1]) );
}
};
getBitmask();
function<bool (set<int>, int)> check_ok = [&] (set<int>ignoring, int howMany){
if(howMany < 0)return false;
bitset<5005>aux = bitmask;
for(auto i : important){
if(ignoring.find(i) != ignoring.end()) continue;
if(nodes[i].rep != i)continue;
int node = findd(i);
aux = ((aux << nodes[node].cnt_color[0]) |(aux << nodes[node].cnt_color[1]) );
}
if(aux[howMany])return true;
return false;
};
auto check_and_erase = [&] (int u){
if(nodes[u].rep != u){
important.erase(u);
}
};
for(auto i : exec){
int u = i.ff, v = i.ss;
//cout << "exec " << i.ff << " " << i.ss << endl;
u = findd(u), v = findd(v);
if(u == v)continue;
int c1 = nodes[i.ff].color, c2 = nodes[i.ss].color;
int cnt0 = nodes[u].cnt_color[0], cnt1 = nodes[u].cnt_color[1];
if(c1 == c2){
cnt0 += nodes[v].cnt_color[1];
cnt1 += nodes[v].cnt_color[0];
}else{
cnt0 += nodes[v].cnt_color[0];
cnt1 += nodes[v].cnt_color[1];
}
if(check_ok({u, v}, n - cnt0)){
// puedo hacer la union.
#ifdef LOCAL
cout << i.ff << " en diferente a " << i.ss << endl;
#endif
if(c1 == c2){
// swapeo colores
for(auto i : nodes[v].group){
nodes[i].color = 1 - nodes[i].color;
}
swap(nodes[v].cnt_color[0], nodes[v].cnt_color[1]);
}
joinn(u, v);
check_and_erase(u);
check_and_erase(v);
}else{
#ifdef LOCAL
cout << i.ff << " en el mismo que " << i.ss << endl;
#endif
if(c1 != c2){
// swapeo colores
for(auto i : nodes[v].group){
nodes[i].color = 1 - nodes[i].color;
}
swap(nodes[v].cnt_color[0], nodes[v].cnt_color[1]);
}
joinn(u, v);
check_and_erase(u);
check_and_erase(v);
}
}
}
int main(){
#ifdef LOCAL
freopen("in","r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 1 ; i < 2 * n ; i ++){
friends.pb({i, i + 1});
}
for(int i = 0 ; i < m ; i ++){
int a, b;
cin >> a >> b;
friends.pb({a, b});
}
for(int i = 1 ; i <= 2 * n ; i ++){
nodes[i].rep = i;
nodes[i].color = 0;
nodes[i].cnt_color[0] = 1;
nodes[i].cnt_color[1] = 0;
nodes[i].group.pb(i);
}
set<int>allRep;
auto restart = [&] (){
exec.clear();
allRep.clear();
};
restart();
while(!friends.empty()){
allRep.insert(findd(friends.back().ff));
allRep.insert(findd(friends.back().ss));
exec.pb(friends.back());
friends.pop_back();
if(allRep.size() > sq){
solve(allRep);
restart();
}
}
solve(allRep);
for(int i = 1 ; i <= 2 * n ; i ++){
cout << nodes[i].color ;
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
2 4 1 3 2 4 1 4 1 2