QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328108#837. Giant PenguinExplodingKonjacWA 0ms7900kbC++207.0kb2024-02-15 17:18:522024-02-15 17:18:52

Judging History

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

  • [2024-02-15 17:18:52]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7900kb
  • [2024-02-15 17:18:52]
  • 提交

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
{
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
	~FastOutput() { flush(); }
	void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
	void put(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	FastOutput &operator <<(char x)
	{ return put(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;put(*(s++)));return *this; }
	FastOutput &operator <<(const std::string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
	{
		if(x<0) return put('-'),(*this)<<(-x);
		char stk[std::numeric_limits<T>::digits10+1],*top=stk;
		do *(top++)=x%10+'0',x/=10; while(x);
		while(top!=stk) put(*(--top));
		return *this;
	}
	template<typename ...T>
	void writesp(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
	template<typename ...T>
	void writeln(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
 private:
	bool __eof;
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char get()
	{
		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 unget(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf[--buf_p]=c;
#else
		ungetc(c,target);
#endif
	}
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=get()));return *this; }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=get()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=get();
		for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
		return unget(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=get()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=get());
		return unget(*s),*s='\0',*this;
	}
	FastInput &operator >>(std::string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=get()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		unget(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &&...x)
	{ std::initializer_list<int>{((*this)>>(x),0)...}; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> 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;
constexpr int INF=1e9;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif

constexpr int MAXN=1e5;

int n,m,q,K;
vector<int> G1[MAXN+5],G2[MAXN+5];

int fa[MAXN+5];
inline int findFa(int x)
{ return x!=fa[x]?fa[x]=findFa(fa[x]):x; }
inline void merge(int x,int y)
{ fa[findFa(y)]=findFa(x); }

int wgt[MAXN+5],anc[MAXN+5],bel[MAXN+5],ord[MAXN+5];
int tot,id1[MAXN+5][20],id2[MAXN+5][20],mn[MAXN+5];
bool vis[MAXN+5];
vector<int> dis[10*MAXN+5],spc[MAXN+5];
void getWeight(int u,int fa=0)
{
	int x=0,y=0;
	for(auto &v: G2[u])
	{
		if(v==fa) continue;
		getWeight(v,u);
		x|=(wgt[v]&y),y|=wgt[v];
	}
	int k=(x?__lg(x)+1:0);
	wgt[u]=((y>>k)+1)<<k;
}
void dfs1(int u,int tp,vector<int> &V,int fa=0)
{
	V.push_back(u);
	bel[u]=tp;
	for(auto &v: G2[u])
		if(v!=fa && !vis[v])
			dfs1(v,fa?tp:v,V,u);
}
void dfs2(int u,vector<int> &res,int fa=0)
{
	bool fl=true;
	for(auto &v: G1[u])
		if(u<v && bel[v] && bel[u]!=bel[v])
			fl=true;
	if(fl) res.push_back(u);
	for(auto &v: G2[u])
		if(v!=fa && !vis[v])
			dfs2(v,res,u);
}
void bfs(int S,int d,int sz)
{
	auto &res=dis[id2[S][d]];
	res.assign(sz,INF);
	queue<int> q;
	q.push(S),res[id1[S][d]]=0;
	while(!q.empty())
	{
		int u=q.front(),du=res[id1[u][d]];
		q.pop();
		for(auto &v: G1[u])
		{
			int &dv=res[id1[v][d]];
			if(du+1>=dv) continue;
			dv=du+1,q.push(v);
		}
	}
}

int main()
{
	qin>>n>>m>>K;
	iota(fa+1,fa+n+1,1);
	for(int i=1;i<=m;i++)
	{
		int u,v;
		qin>>u>>v;
		G1[u].push_back(v);
		G1[v].push_back(u);
		if(findFa(u)!=findFa(v))
		{
			G2[u].push_back(v);
			G2[v].push_back(u);
			merge(u,v);
		}
	}
	getWeight(1);
	for(int i=1;i<=n;i++) wgt[i]=__builtin_ctz(wgt[i]);
	iota(ord+1,ord+n+1,1);
	sort(ord+1,ord+n+1,[](int x,int y){
		return wgt[x]<wgt[y];
	});
	for(int i=1;i<=n;i++)
	{
		int s=ord[i],d=wgt[s],mn=1e9;
		vector<int> V;
		auto search=[&](auto &self,int u,int fa)->void {
			for(auto &v: G2[u])
			{
				if(v==fa) continue;
				if(wgt[v]<wgt[s]) self(self,v,u);
				else if(wgt[v]<mn) mn=wgt[v],anc[s]=v;
			}
		};
		search(search,s,s);
		dfs1(s,s,V),dfs2(s,spc[s]);
		for(int j=0;j<(int)V.size();j++)
			id1[V[j]][d]=j;
		for(auto &j: spc[s])
			id2[j][d]=++tot,bfs(j,d,V.size());
		vis[s]=true;
	}
	fill(mn+1,mn+tot+1,INF);
	qin>>q;
	while(q--)
	{
		int opt,u;
		qin>>opt>>u;
		if(opt==1)
		{
			for(int i=u;i;i=anc[i])
			{
				int k=wgt[i];
				for(auto &v: spc[i])
				{
					int x=id2[v][k];
					mn[x]=min(mn[x],dis[x][id1[u][k]]);
				}
			}
		}
		else
		{
			int ans=INF;
			for(int i=u;i;i=anc[i])
			{
				int k=wgt[i];
				for(auto &v: spc[i])
				{
					int x=id2[v][k];
					ans=min(ans,dis[x][id1[u][k]]+mn[x]);
				}
			}
			qout<<ans<<'\n';
		}
	}
	return 0;
}
/*
5 4 0
1 2
2 3
3 4
4 5
7
1 1
1 5
2 1
2 2
2 3
2 4
2 5

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 7900kb

input:

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

output:

0
0
0
0
0

result:

wrong answer 2nd numbers differ - expected: '1', found: '0'