QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#122660#6413. Classical Graph Theory ProblemExplodingKonjacWA 4ms17712kbC++146.6kb2023-07-10 21:35:462023-07-10 21:35:50

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-10 21:35:50]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:17712kb
  • [2023-07-10 21:35:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<16;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
};

class FastOutput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 private:
	void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	void __write(T x)
	{
		char stk[64],*top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	~FastOutput(){ flush(); }
#endif
	FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	template<typename Iter>
	void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
 private:
	bool __eof;
	char __getc()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		char ch=buf[buf_p++];
#else
		char ch=getc(target);
#endif
		return ~ch?ch:(__eof=true,EOF);
	}
	void __ungetc(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf_p--;
#else
		ungetc(c,target);
#endif
	}
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char peek() { return __getc(); }
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=__getc()));return *this; }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=__getc()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=__getc();
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return __ungetc(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=__getc());
		return __ungetc(*s),*s='\0',*this;
	}
	FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		__ungetc(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &...x)
	{ initializer_list<int>{(this->operator>>(x),0)...}; }
	template<typename Iter>
	void read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif

int T,n,m,deg[200005],lef[200005],ans[200005];
struct Edge{ int u,v; }e[500005];
bool del[200005];
vector<pair<int,int>> g1[200005],g2[200005];

int fa[200005],sum[200005],tag[200005];
inline int findFa(int x)
{ return x!=fa[x]?fa[x]=findFa(fa[x]):x; }
inline void merge(int x,int y)
{ x=findFa(x),y=findFa(y);if(x!=y)fa[y]=x,sum[x]+=sum[y]; }

int main()
{
	qin>>T;
	while(T--)
	{
		qin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			qin>>e[i].u>>e[i].v;
			deg[e[i].u]++;
			deg[e[i].v]++;
		}
		for(int i=1;i<=m;i++)
		{
			auto &[u,v]=e[i];
			if(deg[u]==1) lef[v]++;
			if(deg[v]==1) lef[u]++;
			g1[u].emplace_back(v,i);
			g1[v].emplace_back(u,i);
		}
		for(int i=1;i<=m;i++)
		{
			auto [u,v]=e[i];
			if(deg[u]==1 || deg[v]==1) continue;
			bool fl=true;
			del[i]=true;
			if(deg[u]==2) for(auto &[w,j]: g1[u])
				if(!del[j]) fl&=((++lef[w])<=2);
			if(deg[v]==2) for(auto &[w,j]: g1[v])
				if(!del[j]) fl&=((++lef[w])<=2);
			if(fl) deg[u]--,deg[v]--;
			else
			{
				if(deg[u]==2) for(auto &[w,j]: g1[u])
					if(!del[j]) lef[w]--;
				if(deg[v]==2) for(auto &[w,j]: g1[v])
					if(!del[j]) lef[w]--;
				del[i]=false;
			}
		}
		iota(fa+1,fa+n+1,1);
		fill(tag+1,tag+n+1,1);
		for(int i=1;i<=n;i++)
		{
			if(deg[i]==1)
			{
				if(!lef[i] || ans[i]) continue;
				int u=i,v=g1[i][0].first;
				ans[u]=sum[u]=1;
				ans[v]=sum[v]=-1;
				merge(u,v);
			}
			else if(!lef[i])
			{
				assert(g1[i].size()==2);
				int u=g1[i][0].first,
					v=g1[i][1].first;
				assert(lef[u] && lef[v]);
				if(u>v) swap(u,v);
				g2[v].emplace_back(u,i);
			}
		}
		for(int u=1;u<=n;u++)
		{
			if(deg[u]==1 || !lef[u]) continue;
			int tmp=0;
			for(auto &[v,w]: g2[u]) merge(u,v),tmp+=ans[v];
			sum[u]+=(ans[u]=(tmp<0?1:-1));
			for(auto &[v,w]: g2[u])
				if(ans[u]==ans[v])
					merge(u,v),sum[u]+=(ans[w]=-ans[u]);
			for(auto &[v,w]: g2[u])
				if(ans[u]==ans[v])
					merge(u,v),sum[u]+=(ans[w]=(sum[u]>0?-1:1));
			for(auto &[v,w]: g1[u])
				if(!del[w] && deg[v]==1)
					merge(u,v),sum[u]+=(ans[v]=(sum[u]>0?-1:1));
			assert(abs(sum[u])<=1);
		}
		int tmp=0;
		for(int i=1;i<=n;i++)
		{
			if(findFa(i)!=i) continue;
			if(abs(tmp+sum[i])>abs(tmp-sum[i]))
				tag[i]=-1,sum[i]=-sum[i];
			tmp+=sum[i];
		}
		assert(abs(tmp)<=1);
		for(int i=1;i<=n;i++)
			if(ans[i]*tag[findFa(i)]>0)
				qout<<i<<' ';
		qout<<'\n';
		// do cleaning
		fill(del+1,del+m+1,false);
		for(int i=1;i<=n;i++)
		{
			deg[i]=lef[i]=sum[i]=tag[i]=0;
			g1[i].clear(),g2[i].clear();
		}
	}
	return 0;
}
/*
1
6 7
1 2
1 3
2 3
3 4
4 5
4 6
5 6

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 17712kb

input:

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

output:

1 2 6 
1 3 

result:

wrong answer vertex 3 is not covered (test case 2)