Question: Determine whether a hybrid image is Ouyang.
Process: start to use the EK algorithm to solve the problem and use the adjacent matrix to store the network. However, due to the heavy edge, the problem persists. In the end, there is no way to use the Dinic algorithm to solve the problem.
Solution: the hybrid graph is converted into a directed graph without directed edges, and the network stream is used for solving the problem.
Network Construction idea: add this undirected edge to the proxy network at will when you encounter an undirected edge during reading. The capacity is 1. After reading the data, subtract the exit degree from the inbound degree of each vertex to get x. If x is an odd number, there is definitely no Euler loop. If the difference between the inbound degree and the initial degree (x) is an even number, the network flow is used.
X> 0, an edge with a capacity of x/2 is created between the Source Vertex (s) and the vertex;
X <0, an edge with a capacity of-x/2 is created between the vertex and the sink point (t;
If the network is a full-stream network (maximum flow = sum of the edge capacity connected to the source point), the hybrid graph is European region. Otherwise, it is not.
Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Define maxn205
# Define INF 0x7fffffff
Struct Node
{
Int v;
Int cap;
Int next;
} Node [2, 4050];
Int in [MAXN], out [MAXN];
Int level [MAXN], head [MAXN];
Int que [MAXN];
Int tot;
Int min (int x, int y)
{
Return x <y? X: y;
}
Void init ()
{
Tot = 2;
Memset (in, 0, sizeof (in ));
Memset (out, 0, sizeof (out ));
Memset (head,-1, sizeof (head ));
Memset (node, '/0', sizeof (node ));
}
Void add (int s, int t, int cap)
{
Node [tot]. v = t;
Node [tot]. cap = cap;
Node [tot]. next = head [s];
Head [s] = tot ++;
Node [tot]. v = s;
Node [tot]. cap = 0;
Node [tot]. next = head [t];
Head [t] = tot ++;
}
Int BFS (int s, int t)
{
Int head1 = 0, tail = 0;
Int u;
Memset (level,-1, sizeof (level ));
Que [tail ++] = s;
Level [s] = 0;
While (head1! = Tail)
{
U = que [head1];
Head1 ++;
Int T = head [u];
While (T! =-1)
{
Int v = node [T]. v;
Int cap = node [T]. cap;
If (cap> 0 & level [v] =-1)
{
Level [v] = level [u] + 1;
Que [tail ++] = v;
}
T = node [T]. next;
}
}
Return level [t]! =-1;
}
Int DFS (int s, int mint, int t)
{
Int temp;
If (s = t)
Return mint;
Int T = head [s];
While (T! =-1)
{
Int v = node [T]. v;
Int cap = node [T]. cap;
If (cap> 0 & level [v] = level [s] + 1 & (temp = DFS (v, min (cap, mint), t)> 0)
{
Node [T]. cap-= temp;
Node [T ^ 1]. cap + = temp;
Return temp;
}
T = node [T]. next;
}
Return 0;
}
Int dinic (int s, int t)
{
Int res = 0;
While (BFS (s, t ))
{
While (1)
{
Int a = DFS (s, INF, t );
If (a = 0)
Break;
Res + =;
}
}
Return res;
}
Int main ()
{
Int T, n, m;
Int u, v, d;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
Init ();
While (m --)
{
Scanf ("% d", & u, & v, & d );
If (u = v)
Continue;
Out [u] ++;
In [v] ++;
If (! D)
{
Add (u, v, 1 );
}
}
Int flag = 0;
Int sum = 0;
For (int I = 1; I <= n; I ++)
{
If (abs (in [I]-out [I]) % 2 = 1)
{
Flag = 1;
Break;
}
If (in [I] <out [I])
{
Add (0, I, (out [I]-in [I])/2 );
Sum + = (out [I]-in [I])/2;
}
Else
{
Add (I, n + 1, (in [I]-out [I])/2 );
}
}
If (flag)
{
Printf ("impossible \ n ");
Continue;
}
Int ans = dinic (0, n + 1 );
If (sum = ans)
Printf ("possible \ n ");
Else
Printf ("impossible \ n ");
}
Return 0;
}
Author: kath_y