[***] When will HZOJ come back to me?
Posted by BlueGemini on Sun, 21 Jul 2019 15:47:55 +0200
%%% Immortality.
It's a graph theory. I always thought it was a bipartite graph or some data structure.
Directly speaking, the number is regarded as a node and the card is regarded as a side. From the number of the front side of the card to the side of the negative side with the right of 1 and the opposite side with the right of 0, we can find that we need to reverse several sides to make each point read less than or equal to 1. This is the case. Then each link graph can only be a tree or a base ring tree. The number of points NP and the number of edges NE can be determined by dfs. If ne/2 (bidirectional edges) > np, the graph can not be output directly - 1 - 1 illegally.
1 void dfs(int x,int fa)
2 {
3 vi[x]=1;np++;
4 for(int i=f(x);i;i=n(i))
5 {
6 ne++;
7 if(v(i)!=fa)
8 if(!vi[v(i)])dfs(v(i),x);
9 }
10 }
11 bool pd()
12 {
13 for(int i=1;i<=n*2;i++)
14 if(!vi[i])
15 {
16 np=ne=0,dfs(i,0);
17 if(np<ne/2)return 1;
18 }
19 return 0;
20 }
code implementation
Next, consider that he is a tree.
For a given heel node, the number of flipped edges is to point all points to the father node (you can draw it by hand), which can be obtained by using the tree dp. But do you want to enumerate the root nodes? In fact, it can be used to scan twice and change roots. If the number of edges to be reversed with X is f[x], then f[son] can be updated with f[x]: if the edge weight of X - > son is 1, then f[son]=f[x]-1, otherwise f[son]=f[x]+1. So we can solve the tree situation.
1 int f[MAXN],st,en,ned;
2 void dfs1(int x,int fa)
3 {
4 v[x]=1;f[x]=0;
5 for(int i=f(x);i;i=n(i))
6 if(v(i)!=fa)
7 {
8 if(!v[v(i)])
9 {
10 dfs1(v(i),x);
11 f[x]+=f[v(i)]+w(i);
12 }
13 else st=u(i),en=v(i),ned=i;
14 }
15 }
16 int f2[MAXN];
17 vector<int> tem;
18 void dfs2(int x,int fa)
19 {
20 tem.push_back(f2[x]);
21 for(int i=f(x);i;i=n(i))
22 if(v(i)!=fa && i!=ned && i!=(ned^1))
23 {
24 if(w(i))f2[v(i)]=f2[x]-1;
25 else f2[v(i)]=f2[x]+1;
26 dfs2(v(i),x);
27 }
28 }
code implementation
Look at the base ring tree below.
In dfs, we find a random edge record on the ring, remove it and process it as a tree. Finally, we consider the influence of this edge. Then the paired storage of edges is useful. The number% 2 of edges is the weight from u to v.