QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328157#837. Giant PenguinExplodingKonjacRE 8ms7912kbC++207.0kb2024-02-15 17:40:092024-02-15 17:40:10

Judging History

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

  • [2024-02-15 17:40:10]
  • 评测
  • 测评结果:RE
  • 用时:8ms
  • 内存:7912kb
  • [2024-02-15 17:40:09]
  • 提交

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[11*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,tp?tp:v,V,u);
}
void dfs2(int u,vector<int> &res,int fa=0)
{
	bool fl=!fa;
	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])
		{
			if(vis[v]) continue;
			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,0,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;
		if(spc[s].size()>K+1)
			return qout<<"qweqweqwe",0;
	}
	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],x=id1[u][k];
				for(auto &v: spc[i])
				{
					int y=id2[v][k];
					mn[y]=min(mn[y],dis[y][x]);
				}
			}
		}
		else
		{
			int ans=INF;
			for(int i=u;i;i=anc[i])
			{
				int k=wgt[i],x=id1[u][k];
				for(auto &v: spc[i])
				{
					int y=id2[v][k];
					ans=min(ans,dis[y][x]+mn[y]);
				}
			}
			qout<<ans<<'\n';
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5716kb

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
1
2
1
0

result:

ok 5 number(s): "0 1 2 1 0"

Test #2:

score: 0
Accepted
time: 2ms
memory: 5656kb

input:

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

output:

2
2

result:

ok 2 number(s): "2 2"

Test #3:

score: 0
Accepted
time: 6ms
memory: 5840kb

input:

100 99 0
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52...

output:

99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
99
98
97
9...

result:

ok 199968 numbers

Test #4:

score: 0
Accepted
time: 7ms
memory: 6028kb

input:

100 99 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61...

output:

1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
0
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 199966 numbers

Test #5:

score: 0
Accepted
time: 8ms
memory: 7756kb

input:

100 99 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
2 25
1 26
1 27
1 28
2 29
1 30
1 31
1 32
1 33
2 34
1 35
1 36
2 37
1 38
4 39
1 40
1 41
2 42
2 43
1 44
1 45
2 46
1 47
1 48
1 49
2 50
2 51
1 52
1 53
1 54
2 55
3 56
2 57
1 58
2 59
2 60
3 61...

output:

2
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
3
3
3
4
3
3
3
3
4
3
3
4
3
2
3
3
4
4
3
3
4
3
3
3
4
4
3
3
3
4
4
4
3
4
4
4
3
3
4
3
3
3
4
4
4
3
3
3
3
3
4
3
3
3
3
3
3
3
3
3
3
4
4
3
0
3
3
4
3
4
3
3
4
3
4
2
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
3
3
3
4
3
3
3
3
4
3
3
4
3
2
3
3
4
4
3
3
4
3
3
3
4
...

result:

ok 199964 numbers

Test #6:

score: 0
Accepted
time: 7ms
memory: 7912kb

input:

100 99 0
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
28 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
39 41
41 42
41 43
41 44
44 45
44 46
46 47
44 48
48 49
48 50
50 51
50 52
51...

output:

68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
42
40
39
38
37
36
35
34
33
32
31
32
30
31
31
29
30
30
31
28
29
27
28
26
29
25
24
23
24
22
25
26
21
22
23
20
19
20
18
17
18
16
15
14
16
13
14
15
12
16
11
10
9
10
8
16
7
6
5
6
7
4
7
8
9
10
11
3
10
2
1
0
68
67
66
65
64
...

result:

ok 199966 numbers

Test #7:

score: -100
Runtime Error

input:

100 102 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
...

output:


result: