QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104307#6381. LaLa and HarvestingchenshiTL 0ms0kbC++2.9kb2023-05-10 00:43:332023-05-10 00:43:35

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-10 00:43:35]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-05-10 00:43:33]
  • 提交

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(;i^n;i=lst[i]) inI[i]^=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: 0
Time Limit Exceeded

input:

6 7
1 1 1 1 1 1
0 1
1 2
2 3
2 4
1 5
1 4
0 5
1
2 5

output:


result: