QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104306 | #6382. LaLa and Spirit Summoning | chenshi | TL | 2ms | 3104kb | C++ | 2.9kb | 2023-05-10 00:39:32 | 2023-05-10 00:39:33 |
Judging History
answer
//https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=2C454AA83DC52FE7B02496D6A56F913F?doi=10.1.1.49.8756&rep=rep1&type=pdf
#include<cstdio>
#include<vector>
#include<array>
#include<queue>
using namespace std;
int n,m;vector<int> u,v,c;
struct color{
vector<int> flg,c;
color(int m,vector<int> c_):flg(m),c(c_){}
inline bool chk(int t){return !flg[c[t]];}
inline void ins(int t){flg[c[t]]=1;}
inline void clear(){flg.assign(m,0);}
};
struct rigidity{
int asd;vector<int> u,v,col,lst,frmx,frmt;vector<array<int,2> > pebble;queue<int> qx,qy;
rigidity(int n,int m,vector<int> u_,vector<int> v_):u(u_),v(v_),col(n),lst(n),frmx(m),frmt(m),pebble(n,{-1,-1}){}
inline bool push(int x,int y,int t){
if(pebble[x][t]<0) return true;
++asd;lst[x]=-1;
int id=pebble[x][t];
if((x^u[id]^v[id])==y) return false;
col[x]=col[y]=col[x^u[id]^v[id]]=asd;
queue<int>().swap(qx);queue<int>().swap(qy);
qx.push(x);qy.push(t);
for(;!qx.empty();qx.pop(),qy.pop()){
id=pebble[x=qx.front()][t=qy.front()];
col[y=(x^u[id]^v[id])]=asd;lst[y]=id;
for(int i=0;i<2;++i){
if(pebble[y][i]<0){
for(x=y,t=i;lst[x]>=0;x=y,t=i)
id=lst[x],y=frmx[id],i=frmt[id],pebble[frmx[id]=x][frmt[id]=t]=id;
pebble[x][y]=-1;
return true;
}
id=pebble[y][i];
if(col[y^u[id]^v[id]]^asd) col[y^u[id]^v[id]]=asd,qx.push(y),qy.push(i);
}
}
return false;
}
inline bool chk(int t){return push(u[t],v[t],0)&&push(u[t],v[t],1)&&push(v[t],u[t],0)&&push(v[t],u[t],1);}
inline void ins(int t){push(u[t],v[t],0);pebble[frmx[t]=u[t]][frmt[t]=0]=t;}
inline void clear(){pebble.assign(n,{-1,-1});}
};
template<class M1,class M2>struct MatroidIntersection{
int n;M1 m1;M2 m2;vector<int> inI,lst;queue<int> q;
MatroidIntersection(int n_,M1 m1_,M2 m2_):n(n_),m1(m1_),m2(m2_),inI(n_){}
inline vector<int> RtoL(int t){
vector<int> res;
m1.clear();
for(int i=0;i<n;++i) if(inI[i]&&i-t) m1.ins(i);
for(int i=0;i<n;++i) if(!inI[i]&&lst[i]<0&&m1.chk(i)) res.push_back(i),lst[i]=t;
return res;
}
inline int LtoR(int t){
m2.clear();
for(int i=0;i<2;++i) for(int j=0;j<n;++j) if((j==t||inI[j])&&(lst[j]<0)==i){
if(!m2.chk(j))
if(i){q.push(j);lst[j]=t;return j;}
else return -1;
m2.ins(j);
}
return n;
}
inline bool augment(){
lst.assign(n,-1);
for(q.push(n);!q.empty();q.pop()) for(auto i:RtoL(q.front())) for(int j;(j=LtoR(i))>=0;) if(j==n){
for(;j^n;j=lst[j]) inI[j]^=1;
return true;
}
return false;
}
inline vector<int> slv(){
for(int i=0;i<n;++i) if(m1.chk(i)&&m2.chk(i)) inI[i]=1,m1.ins(i),m2.ins(i);
for(;augment(););
vector<int> res;
for(int i=0;i<n;++i) if(inI[i]) res.push_back(i);
return res;
}
};
int main(){
scanf("%d%d",&n,&m);
u.resize(m);v.resize(m);c.resize(m);
for(int i=0;i<m;++i) scanf("%d%d%d",&u[i],&v[i],&c[i]);
printf("%llu",n*2-MatroidIntersection(m,color(m,c),rigidity(n,m,u,v)).slv().size());
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3048kb
input:
3 3 0 1 0 0 2 0 1 2 0
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 0ms
memory: 2972kb
input:
3 3 0 1 0 0 2 1 1 2 2
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 0ms
memory: 2944kb
input:
4 4 0 1 0 1 2 1 2 3 2 0 3 3
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3044kb
input:
5 4 0 1 0 1 2 1 2 3 2 3 4 3
output:
6
result:
ok 1 number(s): "6"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3044kb
input:
2 0
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 1ms
memory: 2944kb
input:
2 1 0 1 0
output:
3
result:
ok 1 number(s): "3"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3040kb
input:
2 2 0 1 0 0 1 1
output:
3
result:
ok 1 number(s): "3"
Test #8:
score: 0
Accepted
time: 2ms
memory: 2968kb
input:
2 3 0 1 0 0 1 2 0 1 0
output:
3
result:
ok 1 number(s): "3"
Test #9:
score: 0
Accepted
time: 2ms
memory: 3044kb
input:
2 4 0 1 2 0 1 3 0 1 2 0 1 3
output:
3
result:
ok 1 number(s): "3"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3104kb
input:
2 5 0 1 2 0 1 4 0 1 3 0 1 4 0 1 3
output:
3
result:
ok 1 number(s): "3"
Test #11:
score: 0
Accepted
time: 0ms
memory: 2988kb
input:
2 6 0 1 3 0 1 3 0 1 1 0 1 0 0 1 3 0 1 4
output:
3
result:
ok 1 number(s): "3"
Test #12:
score: 0
Accepted
time: 2ms
memory: 3052kb
input:
2 7 0 1 1 0 1 5 0 1 6 0 1 4 0 1 1 0 1 2 0 1 3
output:
3
result:
ok 1 number(s): "3"
Test #13:
score: 0
Accepted
time: 2ms
memory: 3008kb
input:
2 8 0 1 2 0 1 5 0 1 1 0 1 4 0 1 2 0 1 1 0 1 1 0 1 1
output:
3
result:
ok 1 number(s): "3"
Test #14:
score: 0
Accepted
time: 2ms
memory: 3012kb
input:
2 9 0 1 8 0 1 7 0 1 0 0 1 7 0 1 4 0 1 1 0 1 2 0 1 8 0 1 5
output:
3
result:
ok 1 number(s): "3"
Test #15:
score: 0
Accepted
time: 2ms
memory: 2964kb
input:
2 10 0 1 0 0 1 7 0 1 8 0 1 5 0 1 8 0 1 7 0 1 8 0 1 3 0 1 4 0 1 3
output:
3
result:
ok 1 number(s): "3"
Test #16:
score: 0
Accepted
time: 2ms
memory: 2976kb
input:
3 0
output:
6
result:
ok 1 number(s): "6"
Test #17:
score: 0
Accepted
time: 2ms
memory: 2972kb
input:
3 1 1 2 0
output:
5
result:
ok 1 number(s): "5"
Test #18:
score: 0
Accepted
time: 1ms
memory: 2968kb
input:
3 2 1 2 1 1 2 1
output:
5
result:
ok 1 number(s): "5"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3008kb
input:
3 3 1 2 2 1 2 1 0 1 1
output:
4
result:
ok 1 number(s): "4"
Test #20:
score: 0
Accepted
time: 1ms
memory: 3012kb
input:
3 4 1 2 3 0 2 1 1 2 0 1 2 3
output:
4
result:
ok 1 number(s): "4"
Test #21:
score: -100
Time Limit Exceeded
input:
3 5 1 2 0 1 2 2 0 1 4 0 2 0 0 1 0