OJ-Problems-Source/.ACM-Templates/TXTs/图论模板.txt

2264 lines
74 KiB
Plaintext
Raw Normal View History

2016-11-22 09:38:35 +08:00
==========ͼ<><CDBC>ģ<EFBFBD><C4A3> LCA=========
//LCA (Least Common Ancestors, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
//<2F><><EFBFBD><EFBFBD>dfs + ST O(nlogn + q)
int dfso[N << 1], cnt; //ŷ<><C5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><>dfs<66><73>, <20><><EFBFBD><EFBFBD>Ϊ2n-1, <20>±<EFBFBD><C2B1><EFBFBD>1<EFBFBD><31>ʼ
int pos[N]; //pos[i]<5D><>ʾ<EFBFBD><CABE>i<EFBFBD><69>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>һ<EFBFBD>γ<EFBFBD><CEB3>ֵ<EFBFBD>λ<EFBFBD><CEBB>
int dep[N << 1]; //ŷ<><C5B7><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int p[N << 1] = { -1}, dp[N << 1][20];
void initRMQ(int n) {
for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + !(i & (i - 1)); dp[i][0] = i; }
for (int j = 1; j <= p[n]; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
dp[i][j] = dep[dp[i][j - 1]] < dep[dp[i + (1 << (j - 1))][j - 1]] ? dp[i][j - 1] : dp[i + (1 << (j - 1))][j - 1];
}
}
}
inline int queryRMQ(int l, int r) {
if (l > r) { swap(l, r); }
int k = p[r - l + 1];
return dep[dp[l][k]] <= dep[dp[r - (1 << k) + 1][k]] ? dp[l][k] : dp[r - (1 << k) + 1][k];
}
void dfs(int u, int p, int d) {
dfso[++cnt] = u; dep[cnt] = d; pos[u] = cnt;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v != p) { dfs(v, u, d + 1); dfso[++cnt] = u; dep[cnt] = d; }
}
}
void initLCA(int rt, int n) {
cnt = 0; dfs(rt, rt, 0); initRMQ(2 * n - 1);
}
inline int queryLCA(int u, int v) { //<2F><>ѯu<D1AF><75>v<EFBFBD><76>LCA<43><41><EFBFBD><EFBFBD>
return dfso[queryRMQ(pos[u], pos[v])];
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(nlogn + qlogn)
const int DEP = 20;
int fa[N][DEP]; //fa[i][j]<5D><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>i<EFBFBD>ĵ<EFBFBD>2^j<><6A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int dep[N]; //<2F><><EFBFBD><EFBFBD>
void bfs(int rt) {
queue<int> que; que.push(rt);
dep[rt] = 0; fa[rt][0] = rt;
while (!que.empty()) {
int u = que.front(); que.pop();
for (int i = 1; i < DEP; i++) { fa[u][i] = fa[fa[u][i - 1]][i - 1]; }
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v != fa[u][0]) { dep[v] = dep[u] + 1; fa[v][0] = u; que.push(v); }
}
}
}
int queryLCA(int u, int v) {
if (dep[u] > dep[v]) { swap(u, v); }
for (int d = dep[v] - dep[u], i = 0; d; d >>= 1, i++) {
if (d & 1) { v = fa[v][i]; }
}
if (u == v) { return u; }
for (int i = DEP - 1; i >= 0; i--) {
if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; }
}
return fa[u][0];
}
//<2F><><EFBFBD><EFBFBD>Tarjan O(n + q)
int n, fa[N], ancestor[N], ans[N];
bool vis[N];
int qhead[N], qto[M], qnxt[M], qtot;
void init() {
qtot = 0; memset(qhead, -1, sizeof(qhead));
memset(ancestor, 0, sizeof(ancestor)); memset(vis, 0, sizeof(vis));
for (int i = 0; i <= n; i++) { fa[i] = i; }
}
void qaddedge(int x, int y) {
qto[qtot] = y; qnxt[qtot] = qhead[x]; qhead[x] = qtot++;
qto[qtot] = x; qnxt[qtot] = qhead[y]; qhead[y] = qtot++;
}
int findfa(int n) { return n == fa[n] ? n : fa[n] = findfa(fa[n]); }
inline void unite(int x, int y) {
x = findfa(x); y = findfa(y);
if (x != y) { fa[y] = x; }
}
void Tarjan_LCA(int u) {
ancestor[u] = u; vis[u] = true;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!vis[v]) { Tarjan_LCA(v); unite(u, v); ancestor[findfa(u)] = u; }
}
for (int i = qhead[u]; ~i; i = qnxt[i]) {
int v = qto[i];
if (vis[v]) { ans[i >> 1] = ancestor[findfa(v)]; }
}
}
==================ͼ<><CDBC>-<2D><>ͨ<EFBFBD><CDA8>==================
//ͼ<>ĸ<EFBFBD><C4B8><EFBFBD><E3A1A2><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//[<5B><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>]
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨͼ<CDA8><CDBC>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BCAF>, ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BCAF>,
//<2F>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>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><D2BB>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD>, <20><>G'Ϊ<><CEAA><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8>ͼ
//˫<><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>(biconnected component), <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ͼ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
//[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
//<2F><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>DFS(u)Ϊu<CEAA><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>Ϊ<EFBFBD><CEAA>)<29>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>Low(u)Ϊu<CEAA><75>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><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CDBC>ǿ<EFBFBD><C7BF>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//Kosaraju<6A>㷨 O(V + E)
vector<int> e[N], re[N], pos;
bool vis[N];
int belong[N], num[N], scc, cnt; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < (int)e[u].size(); i++) {
if (!vis[e[u][i]]) { dfs(e[u][i]); }
}
pos.push_back(u);
}
void rdfs(int u, int k) {
vis[u] = true; belong[u] = k; cnt++;
for (int i = 0; i < (int)re[u].size(); i++) {
if (!vis[re[u][i]]) { rdfs(re[u][i], k); }
}
}
void SCC(int n) {
memset(vis, 0, sizeof(vis)); pos.clear();
for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i); } }
memset(vis, 0, sizeof(vis)); memset(num, 0, sizeof(num)); scc = 0;
for (int i = (int)pos.size() - 1; i >= 0; i--) {
if (!vis[pos[i]]) { cnt = 0; rdfs(pos[i], ++scc); num[scc] = cnt; }
}
}
//Tarjan<61>㷨 O(V + E)
vector<int> e[N];
int low[N], dfn[N], index, belong[N], num[N], scc; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
stack<int> stk;
bool instack[N];
void Tarjan(int u) {
int v; low[u] = dfn[u] = ++index; stk.push(u); instack[u] = true;
for (int i = 0; i < (int)e[u].size(); i++) {
int v = e[u][i];
if (!dfn[v]) { Tarjan(v); low[u] = min(low[u], low[v]); }
else if (instack[v] && low[u] > dfn[v]) { low[u] = dfn[v]; }
}
if (low[u] == dfn[u]) {
scc++;
do {
v = stk.top(); stk.pop();
instack[v] = false; belong[v] = scc; num[scc]++;
} while (v != u);
}
}
void SCC(int n) {
memset(dfn, 0, sizeof(dfn)); memset(num, 0, sizeof(num));
memset(instack, 0, sizeof(instack)); index = scc = 0;
while (!stk.empty()) { stk.pop(); }
for (int i = 0; i < n; i++) { if (!dfn[i]) { Tarjan(i); } }
}
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Tarjan<61>㷨 O(V + E)
int low[N], dfn[N], index, addblock[N], bridge;
bool instack[N], cut[N], ecut[M];
stack<int> stk;
void Tarjan(int u, int p) {
int son = 0; low[u] = dfn[u] = ++index;
stk.push(u); instack[u] = true;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == p) { continue; }
if (!dfn[v]) {
Tarjan(v, u); son++;
low[u] = min(low[u], low[v]);
if (u != p && low[v] >= dfn[u]) { //<2F><><EFBFBD><EFBFBD>
cut[u] = true; addblock[u]++;
}
if (low[v] > dfn[u]) { //<2F><><EFBFBD><EFBFBD>
bridge++; ecut[i] = ecut[i ^ 1] = true;
}
} else if (low[u] > dfn[v]) { low[u] = dfn[v]; }
}
if (u == p && son > 1) { cut[u] = true; }
if (u == p) { addblock[u] = son - 1; }
instack[u] = false; stk.pop();
}
void CUT(int n) {
memset(dfn, 0, sizeof(dfn)); memset(addblock, 0, sizeof(addblock));
memset(instack, 0, sizeof(instack)); memset(cut, 0, sizeof(cut)); memset(ecut, 0, sizeof(ecut));
while (!stk.empty()) { stk.pop(); } index = bridge = 0;
for (int i = 0; i < n; i++) { if (!dfn[i]) { Tarjan(i, i); } }
}
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD>ı<EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>Ժ<EFBFBD>, <20><><EFBFBD>ű<EFBFBD>ɾ<EFBFBD><C9BE>, ԭͼ<D4AD><CDBC><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>, <20><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//<2F>Ų<EFBFBD><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ıߺ<C4B1>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//Tarjan<61>㷨 O(V + E)
int low[N], dfn[N], index, belong[N], bridge, block;
bool instack[N], ecut[M];
stack<int> stk;
void Tarjan(int u, int p) {
low[u] = dfn[u] = ++index; stk.push(u); instack[u] = true;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == p) { continue; }
if (!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) { //<2F><><EFBFBD><EFBFBD>
bridge++; ecut[i] = ecut[i ^ 1] = true;
}
} else if (low[u] > dfn[v]) { low[u] = dfn[v]; }
}
if (low[u] == dfn[u]) {
int v; block++;
do {
v = stk.top(); stk.pop(); instack[v] = false; belong[v] = block;
} while (u != v);
}
}
void EBCC(int n) {
memset(dfn, 0, sizeof(dfn)); memset(instack, 0, sizeof(instack)); memset(cut, 0, sizeof(cut));
while (!stk.empty()) { stk.pop(); } index = bridge = block = 0;
for (int i = 0; i < n; i++) { if (!dfn[i]) { Tarjan(i, -1); } }
}
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD>ĵ<EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ڵ<EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>, ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>о<EFBFBD><D0BE><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ջ, <20><EFBFBD><E6B4A2>ǰ˫<C7B0><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼʱ, ÿ<>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>֦<EFBFBD>߻<EFBFBD><DFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28>Ǻ<EFBFBD><C7BA><EFBFBD><EFBFBD><EFBFBD>), <20>Ͱ<EFBFBD><CDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC><EFBFBD>ջ<EFBFBD><D5BB>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijʱ<C4B3><CAB1><EFBFBD><EFBFBD>DFS(u)<=Low(v), ˵<><CBB5>u<EFBFBD><75>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ͬʱ<CDAC>ѱߴ<D1B1>ջ<EFBFBD><D5BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>,
//ֱ<><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD>(u,v), ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>, <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
//Tarjan<61>㷨 O(V + E)
int low[N], dfn[N], index, belong[N], block; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
stack<int> stk;
bool instack[N];
void Tarjan(int u, int p) {
low[u] = dfn[u] = ++index;
stk.push(u); instack[u] = true;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == p) { continue; }
if (!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) { //uΪ<75><CEAA><EFBFBD><EFBFBD>
int vv; block++;
do {
vv = stk.top(); stk.pop(); instack[vv] = false;
belong[vv] = block;
} while (vv != v);
}
} else if (instack[v] && low[u] > dfn[v]) { low[u] = dfn[v]; }
}
}
void BCC(int n) {
memset(dfn, 0, sizeof(dfn)); memset(instack, 0, sizeof(instack));
while (!stk.empty()) { stk.pop(); } index = block = 0;
for (int i = 0; i < n; i++) { if (!dfn[i]) { Tarjan(i, -1); } }
}
//<2F><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨͼ
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>, Ȼ<><C8BB>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Щ<EFBFBD>ű<EFBFBD>, ʣ<>µ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><E9B6BC>һ<EFBFBD><D2BB>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8>ͼ
//<2F><>ÿ<EFBFBD><C3BF>˫<EFBFBD><CBAB>ͨ<EFBFBD><CDA8>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>ٰ<EFBFBD><D9B0>ű߼ӻ<DFBC><D3BB><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBF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BFBD><EFBFBD><EFBFBD><EFBFBD><E3B5BD><EFBFBD>ȵ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>,
//<2F><>Ϊһ<CEAA><D2BB><EFBFBD>γɵĻ<C9B5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ͨ<EFBFBD>ġ<EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>ڵ<EFBFBD>, <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ǡ<><C7A1><EFBFBD><EFBFBD>(leaf + 1) / 2<><32>, <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
=============================ͼ<><CDBC><><C6A5>============================
//ͼ<>۵<DBB5>߼<EFBFBD><DFBC>Ͷ<EFBFBD><CDB6><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><EFBFBD><E3B8B2>: <20><EFBFBD>Ǽ<EFBFBD><C7BC><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>㼯, ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>˵<EFBFBD><CBB5>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD>
//<2F>߸<EFBFBD><DFB8><EFBFBD>: <20>߸<EFBFBD><DFB8>Ǽ<EFBFBD><C7BC><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߼<EFBFBD>, ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>е<D0B5><EFBFBD><EBBCAF><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ڽ<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>㼯, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B2BB><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><56><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>: <20>ż<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>㼯, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>: <20>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߼<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><DFB2>ڽ<EFBFBD>
//֧<>伯: ֧<><EFBFBD><E4BCAF>һ<EFBFBD><D2BB><EFBFBD>㼯, ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD>
//<2F><>֧<EFBFBD>伯: <20><>֧<EFBFBD><EFBFBD><E4BCAF>һ<EFBFBD><D2BB><EFBFBD>߼<EFBFBD>, ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڽӱ<DABD><D3B1>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD>
//<2F><>С·<D0A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20>þ<EFBFBD><C3BE><EFBFBD><EFBFBD>ٵIJ<D9B5><C4B2><EFBFBD><E0BDBB><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޻<EFBFBD>ͼG<CDBC><47><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>, <20><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>·<EFBFBD><C2B7>, ·<><C2B7><EFBFBD>ij<EFBFBD><C4B3>ȿ<EFBFBD><C8BF><EFBFBD>Ϊ0(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
//ƥ<><C6A5>: ƥ<><C6A5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>߼<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC>еı<D0B5><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>
//<2F><>ƥ<EFBFBD><C6A5><EFBFBD>еĵ<D0B5><C4B5><EFBFBD>Ϊƥ<CEAA><C6A5><EFBFBD><EFBFBD><EFBFBD>򱥺͵㣻<CDB5><E3A3BB>֮, <20><>Ϊδƥ<CEB4><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>͵<EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(alternating path): ͼ<><CDBC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, һ<><D2BB><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>, һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(augmenting path): <20><>һ<EFBFBD><D2BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>յ㶼Ϊδƥ<CEB4><C6A5><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>(maximum matching): <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƥ<EFBFBD><C6A5>
//ƥ<><C6A5><EFBFBD><EFBFBD>(matching number): <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>Ĵ<EFBFBD>С
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>(perfect matching): ƥ<><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>ƥ<EFBFBD><C6A5>
//<2F>걸ƥ<EAB1B8><C6A5>(complete matching): ƥ<><C6A5><EFBFBD>˶<EFBFBD><CBB6><EFBFBD>ͼ<EFBFBD><CDBC>С<EFBFBD><D0A1><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD>ͼX, Y<><59>С<EFBFBD><D0A1><EFBFBD>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>ƥ<EFBFBD><C6A5>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB6A8>: һ<><D2BB>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><E3B7A8><EFBFBD>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB6A8>: һ<><D2BB>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD> = <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD> + <20>ұ<EFBFBD>δƥ<CEB4><C6A5><EFBFBD><EFBFBD>
//<2F><>С<EFBFBD>߸<EFBFBD><DFB8><EFBFBD> = ͼ<>е<EFBFBD><D0B5>ĸ<EFBFBD><C4B8><EFBFBD> - <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD> = <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
//Hungary + dfs
//<2F>ڽӾ<DABD><D3BE><EFBFBD> O(V^2)
int uN, vN, match[N]; //<2F><><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>Ŀ
bool g[N][N], check[N];
bool dfs(int u) {
for (int v = 0; v < vN; v++) {
if (g[u][v] && !check[v]) {
check[v] = true;
if (match[v] == -1 || dfs(match[v])) {
match[v] = u; match[u] = v; return true;
}
}
}
return false;
}
//<2F>ڽӱ<DABD> O(V*E)
int head[N], to[M], nxt[M], tot, uN, match[N];
bool check[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
bool dfs(int u) {
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!check[v]) {
check[v] = true;
if (match[v] == -1 || dfs(match[v])) {
match[v] = u; match[u] = v; return true;
}
}
}
return false;
}
int Hungary() {
memset(match, -1, sizeof(match));
int res = 0;
for (int u = 0; u < uN; u++) {
if (match[u] == -1) {
memset(check, 0, sizeof(check));
if (dfs(u)) { res++; }
}
}
return res;
}
//Hungary + bfs + <20>ڽӾ<DABD><D3BE><EFBFBD> O(V*E)
bool g[N][N];
int uN, vN, match[N], check[N], pre[N];
int Hungary() {
memset(match, -1, sizeof(match));
memset(check, -1, sizeof(check));
queue<int> que;
int res = 0;
for (int i = 0; i < uN; i++) {
if (match[i] == -1) {
while (!que.empty()) { que.pop(); }
que.push(i); pre[i] = -1;
bool flag = false;
while (!que.empty() && !flag) {
int u = que.front(); que.pop();
for (int v = 0; v < vN && !flag; v++) {
if (g[u][v] && check[v] != i) {
check[v] = i; que.push(match[v]);
if (~match[v]) { pre[match[v]] = u; }
else {
flag = true;
for (int a = u, b = v; ~a;) {
int t = match[a]; match[a] = b; match[b] = a; a = pre[a]; b = t;
}
}
}
}
}
if (~match[i]) { res++; }
}
}
return res;
}
//Hungary + bfs + <20>ڽӱ<DABD> O(V*E)
int head[N], to[M], nxt[M], tot, uN, match[N], check[N], pre[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
int Hungary() {
memset(match, -1, sizeof(match));
memset(check, -1, sizeof(check));
queue<int> que;
int res = 0;
for (int i = 0; i < uN; i++) {
if (match[i] == -1) {
while (!que.empty()) { que.pop(); }
que.push(i); pre[i] = -1;
bool flag = false;
while (!que.empty() && !flag) {
int u = que.front(); que.pop();
for (int i = head[u]; ~i && !flag; i = nxt[i]) {
int v = to[i];
if (check[v] != i) {
check[v] = i; que.push(match[v]);
if (~match[v]) { pre[match[v]] = u; }
else {
flag = true;
for (int a = u, b = v; ~a;) {
int t = match[a]; match[a] = b; match[b] = a; a = pre[a]; b = t;
}
}
}
}
}
if (~match[i]) { res++; }
}
}
return res;
}
//Hopcroft-Karp + vector + O(sqrt(V)*E)
const int INF = 0x3f3f3f3f;
vector<int> g[N];
int uN, matchx[N], matchy[N], dx[N], dy[N], dis;
bool check[N];
bool SearchP() {
memset(dx, -1, sizeof(dx));
memset(dy, -1, sizeof(dy));
queue<int> que; dis = INF;
for (int i = 0; i < uN; i++) { if (matchx[i] == -1) { dx[i] = 0; que.push(i); } }
while (!que.empty()) {
int u = que.front(); que.pop();
if (dx[u] > dis) { break; }
for (size_t i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (dy[v] == -1) {
dy[v] = dx[u] + 1;
if (matchy[v] == -1) { dis = dy[v]; }
else { dx[matchy[v]] = dy[v] + 1; que.push(matchy[v]); }
}
}
}
return dis != INF;
}
bool dfs(int u) {
for (size_t i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!check[v] && dy[v] == dx[u] + 1) {
check[v] = true;
if (~matchy[v] && dy[v] == dis) { continue; }
if (matchy[v] == -1 || dfs(matchy[v])) {
matchy[v] = u; matchx[u] = v; return true;
}
}
}
return false;
}
int HK() {
memset(matchx, -1, sizeof(matchx));
memset(matchy, -1, sizeof(matchy));
int res = 0;
while (SearchP()) {
memset(check, 0, sizeof(check));
for (int i = 0; i < uN; i++) {
if (matchx[i] == -1 && dfs(i)) { res++; }
}
}
return res;
}
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
//Kuhn-Munkers + <20>ڽӾ<DABD><D3BE><EFBFBD> O(uN^2*vN)
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>СȨƥ<C8A8><C6A5>,<2C>ɽ<EFBFBD>Ȩֵȡ<D6B5><EFBFBD><E0B7B4>,<2C><><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><E0B7B4> <20><><EFBFBD>ı<EFBFBD><C4B1>Ŵ<EFBFBD>0<EFBFBD><30>ʼ
const int INF = 0x3f3f3f3f;
int uN, vN, g[N][N];
int matchy[N], lx[N], ly[N]; //y<>и<EFBFBD><D0B8><EFBFBD>ƥ<EFBFBD><C6A5>״̬, <20><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
int slack[N]; //<2F>ɳ<EFBFBD><C9B3><EFBFBD><EFBFBD><EFBFBD>
bool visx[N], visy[N];
bool dfs(int u) {
visx[u] = true;
for (int v = 0; v < vN; v++) {
if (visy[v]) { continue; }
int tmp = lx[u] + ly[v] - g[u][v];
if (tmp == 0) {
visy[v] = true;
if (matchy[v] == -1 || dfs(matchy[v])) { matchy[v] = u; return true; }
} else if (slack[v] > tmp) { slack[v] = tmp; }
}
return false;
}
int KM() {
memset(matchy, -1, sizeof(matchy));
memset(ly, 0, sizeof(ly));
for (int i = 0; i < uN; i++) { lx[i] = *max_element(g[i], g[i] + vN); }
for (int u = 0; u < uN; u++) {
memset(slack, 0x3f, sizeof(slack));
for (;;) {
memset(visx, 0, sizeof(visx));
memset(visy, 0, sizeof(visy));
if (dfs(u)) { break; }
int d = INF;
for (int i = 0; i < vN; i++) { if (!visy[i] && d > slack[i]) { d = slack[i]; } }
for (int i = 0; i < uN; i++) { if (visx[i]) { lx[i] -= d; } }
for (int i = 0; i < vN; i++) { if (visy[i]) { ly[i] += d; } else { slack[i] -= d; } }
}
}
int res = 0;
for (int i = 0; i < vN; i++) { if (matchy[i] != -1) { res += g[matchy[i]][i]; } }
return res;
}
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
int uN, vN, g[N][N], match[N][N]; //match[i][0]Ϊ<><CEAA><EFBFBD><EFBFBD>
int num[N]; //<2F>ұ<EFBFBD><D2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>
bool check[N];
bool dfs(int u) {
for (int v = 0; v < vN; v++) {
if (g[u][v] && !check[v]) {
check[v] = true;
if (match[v][0] < num[v]) { match[v][++match[v][0]] = u; return true; }
for (int i = 1; i <= num[0]; i++) {
if (dfs(match[v][i])) { match[v][i] = u; return true; }
}
}
}
return false;
}
int Hungary() {
int res = 0;
for (int i = 0; i < vN; i++) { match[i][0] = 0; }
for (int u = 0; u < uN; u++) {
memset(check, false, sizeof(check));
res += dfs(u);
}
return res;
}
//һ<><D2BB>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
//<2F>ڽӾ<DABD><D3BE><EFBFBD> O(V^3)
int n, match[N];
bool g[N][N], check[N];
bool aug(int now) {
bool ret = false; check[now] = true;
for (int i = 0; i < n; i++) {
if (!check[i] && g[now][i]) {
if (match[i] == -1) { match[now] = i; match[i] = now; ret = true; }
else {
check[i] = true;
if (aug(match[i])) { match[now] = i, match[i] = now, ret = true; }
check[i] = false;
}
if (ret) { break; }
}
}
check[now] = false; return ret;
}
//<2F>ڽӱ<DABD> O(V*E)
int head[N], to[M], nxt[M], tot, match[N];
bool check[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
bool aug(int now) {
bool ret = false; check[now] = true;
for (int i = head[now]; ~i; i = nxt[i]) {
int v = to[i];
if (!check[v]) {
if (match[v] == -1) { match[now] = v; match[v] = now; ret = true; }
else {
check[v] = true;
if (aug(match[v])) { match[now] = v; match[v] = now; ret = true; }
check[v] = false;
}
if (ret) { break; }
}
}
check[now] = false; return ret;
}
int graphMatch() {
memset(match, -1, sizeof(match));
memset(check, 0, sizeof(check));
for (int i = 0, j = n; i < n && j >= 2; i++) {
if (match[i] == -1 && aug(i)) { i = -1, j -= 2; }
}
int ret = 0;
for (int i = 0; i < n; i++) { ret += (match[i] != -1); }
return ret >> 1;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> + vector O(V^3) <20><><EFBFBD>ı<EFBFBD><C4B1>Ŵ<EFBFBD>0<EFBFBD><30>ʼ
int n, match[N], fa[N], nxt[N], mark[N], vis[N], t;
vector<int> e[N]; queue<int> que;
int findfa(int n) { return n == fa[n] ? n : fa[n] = findfa(fa[n]); }
void unite(int x, int y) { fa[findfa(x)] = findfa(y); }
int lca(int x, int y) {
for (t++;; swap(x, y)) {
if (x == -1) { continue; }
if (vis[x = findfa(x)] == t) { return x; }
vis[x] = t; x = match[x] == -1 ? -1 : nxt[match[x]];
}
}
void group(int a, int p) {
for (int b, c; a != p; unite(a, b), unite(b, c), a = c) {
b = match[a]; c = nxt[b];
if (findfa(c) != p) { nxt[c] = b; }
if (mark[b] == 2) { mark[b] = 1; que.push(b); }
if (mark[c] == 2) { mark[c] = 1; que.push(c); }
}
}
void aug(int st) {
for (int i = 0; i < n; i++) { fa[i] = i; }
memset(nxt, -1, sizeof(nxt)); memset(vis, -1, sizeof(vis)); memset(mark, 0, sizeof(mark));
while (!que.empty()) { que.pop(); } que.push(st); mark[st] = 1;
while (match[st] == -1 && !que.empty()) {
int u = que.front(); que.pop();
for (int i = 0; i < (int)e[u].size(); i++) {
int v = e[u][i];
if (v == match[u] || findfa(u) == findfa(v) || mark[v] == 2) { continue; }
if (mark[v] == 1) {
int p = lca(u, v);
if (findfa(u) != p) { nxt[u] = v; }
if (findfa(v) != p) { nxt[v] = u; }
group(u, p); group(v, p);
} else if (match[v] == -1) {
nxt[v] = u;
for (int j = v, k, l; ~j; j = l) { k = nxt[j]; l = match[k]; match[j] = k; match[k] = j; }
break;
} else { nxt[v] = u; que.push(match[v]); mark[match[v]] = 1; mark[v] = 2; }
}
}
}
int solve(int n) {
memset(match, -1, sizeof(match)); t = 0; int ret = 0;
for (int i = 0; i < n; i++) { if (match[i] == -1) { aug(i); } }
for (int i = 0; i < n; i++) { ret += (match[i] > i); }
return ret;
}
//ural 1099
int main() {
int u, v;
scanf("%d", &n);
while (~scanf("%d%d", &u, &v)) { e[--u].push_back(--v); e[v].push_back(u); }
int ans = solve(n);
printf("%d\n", ans * 2);
for (int u = 0; u < n; u++) {
if (u < match[u]) { printf("%d %d\n", u + 1, match[u] + 1); }
}
}
============ͼ<><CDBC>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>==========
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Prim + <20>ڽӾ<DABD><D3BE><EFBFBD> O(V^2)
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
int n; wtype mp[N][N], cost[N];
bool vis[N];
wtype Prim(int src) {
memset(vis, 0, sizeof(vis)); memcpy(cost, mp[src], sizeof(mp[src]));
wtype ret = 0; vis[src] = true;
for (int j = 1; j < n; j++) {
int u = -1; wtype mn = INF;
for (int i = 0; i < n; i++) {
if (!vis[i] && mn > cost[i]) { mn = cost[i]; u = i; }
}
if (mn == INF) { return -1; } //ԭͼ<D4AD><CDBC><EFBFBD><EFBFBD>ͨ
vis[u] = true; ret += mn;
for (int v = 0; v < n; v++) {
if (!vis[v] && cost[v] > mp[u][v]) { cost[v] = mp[u][v]; }
}
}
return ret;
}
//Prim + priority_queue + <20>ڽӱ<DABD> O(ElogV)
typedef int wtype;
int head[N], to[M], nxt[M], tot; wtype len[M], cost[N];
bool vis[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y, wtype z) { to[tot] = y; len[tot] = z; nxt[tot] = head[x]; head[x] = tot++; }
struct Node {
int v; wtype w;
bool operator<(const Node &r)const { return w > r.w; }
};
wtype Prim(int src) {
memset(cost, 0x3f, sizeof(cost)); memset(vis, 0, sizeof(vis));
wtype ret = 0;
priority_queue<Node> que; que.push((Node) {src, cost[src] = 0});
while (!que.empty()) {
int u = que.top().v; que.pop();
if (vis[u]) { continue; }
vis[u] = true; ret += cost[u];
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!vis[v] && cost[v] > len[i]) { cost[v] = len[i]; que.push((Node) {v, cost[v]}); }
}
}
return ret;
}
//Kruskal + <20>ڽӱ<DABD> O(ElogE)
typedef int wtype;
struct Edge {
int u, v; wtype w;
bool operator<(const Edge &r)const { return w < r.w; }
} edge[M];
int n, fa[N], tot; //<2F>ӱ<EFBFBD>ǰ<EFBFBD><C7B0>ֵΪ0
void addedge(int u, int v, wtype w) { edge[tot].u = u; edge[tot].v = v; edge[tot++].w = w; }
int findfa(int x) { return fa[x] == -1 ? x : fa[x] = findfa(fa[x]); }
wtype Kruskal() {
memset(fa, -1, sizeof(fa));
sort(edge, edge + tot);
int cnt = 0; wtype ret = 0;
for (int i = 0; i < tot; i++) {
int u = edge[i].u, v = edge[i].v, t1 = findfa(u), t2 = findfa(v); wtype w = edge[i].w;
if (t1 != t2) { ret += w; fa[t1] = t2; cnt++; }
if (cnt == n - 1) { break; }
}
if (cnt < n - 1) { return -1; } //<2F><><EFBFBD><EFBFBD>ͨ
return ret;
}
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Prim + <20>ڽӾ<DABD><D3BE><EFBFBD> O(V^2 + E)
//<2F><><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Mx[i][j]<5D><><EFBFBD><EFBFBD>ʾMST<53><54>i<EFBFBD><69>j<EFBFBD><6A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֱ<><D6B1>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>MST<53>еı<D0B5>, <20><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD>ı<EFBFBD>, <20><><EFBFBD>´<EFBFBD><C2B4><EFBFBD>
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
int n, pre[N];
wtype mp[N][N], cost[N], Mx[N][N]; //Mx[i][j]<5D><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>i<EFBFBD><69>j<EFBFBD><6A>·<EFBFBD><C2B7><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ
bool vis[N], used[N][N];
wtype Prim() {
memset(Mx, 0, sizeof(Mx));
memset(pre, 0, sizeof(pre));
memset(vis, 0, sizeof(vis));
memset(used, 0, sizeof(used));
wtype ret = 0; vis[0] = true; pre[0] = -1;
for (int i = 1; i < n; i++) { cost[i] = mp[0][i]; }
for (int j = 1; j < n; j++) {
int u = -1; wtype mn = INF;
for (int j = 0; j < n; j++)
if (!vis[j] && mn > cost[j]) { mn = cost[j]; u = j; }
if (mn == INF) { return -1; } //ԭͼ<D4AD><CDBC><EFBFBD><EFBFBD>ͨ
vis[u] = true; ret += mn;
used[u][pre[u]] = used[pre[u]][u] = true;
for (int v = 0; v < n; v++) {
if (vis[v]) { Mx[v][u] = Mx[u][v] = max(Mx[v][pre[u]], cost[u]); }
if (!vis[v] && cost[v] > mp[u][v]) { cost[v] = mp[u][v]; pre[v] = u; }
}
}
return ret;
}
//Kruskal + <20>ڽӱ<DABD> O(VElogE)
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
struct Edge {
int u, v; wtype w;
bool operator<(const Edge &r)const { return w < r.w; }
} edge[M];
int n, fa[N], path[N], tot; //<2F>ӱ<EFBFBD>ǰ<EFBFBD><C7B0>ֵΪ0
void addedge(int u, int v, wtype w) { edge[tot].u = u; edge[tot].v = v; edge[tot++].w = w; }
int findfa(int x) { return fa[x] == -1 ? x : fa[x] = findfa(fa[x]); }
wtype Kruskal() {
memset(fa, -1, sizeof(fa));
sort(edge, edge + tot);
int cnt = 0; wtype ret = 0;
for (int i = 0; i < tot; i++) {
int u = edge[i].u, v = edge[i].v, t1 = findfa(u), t2 = findfa(v); wtype w = edge[i].w;
if (t1 != t2) { ret += w; fa[t1] = t2; path[cnt++] = i; }
if (cnt == n - 1) { break; }
}
if (cnt < n - 1) { return -1; } //<2F><><EFBFBD><EFBFBD>ͨ
return ret;
}
wtype KruskalSec() {
wtype ret = INF;
for (int x = 0; x < n - 1; x++) {
memset(fa, -1, sizeof(fa));
int cnt = 0; wtype tmp = 0;
for (int i = 0; i < tot; i++) {
if (i != path[x]) {
int u = edge[i].u, v = edge[i].v, t1 = findfa(u), t2 = findfa(v); wtype w = edge[i].w;
if (t1 != t2) { tmp += w; fa[t1] = t2; cnt++; }
if (cnt == n - 1) { if (tmp < ret) { ret = tmp; } break; }
}
}
}
if (ret == INF) { return -1; } //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return ret;
}
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ͼ
//<2F><><EFBFBD><EFBFBD><EFBFBD>㷨 O(VE)
typedef int wtype;
const int N = 1005;
const int M = 40005;
const wtype INF = 0x3f3f3f3f;
struct Edge { int u, v; wtype w; } edge[M];
int n, m, pre[N], id[N], vis[N];
wtype g[N][N], in[N];
wtype Zhuliu(int root) {
wtype res = 0; int u, v;
for (;;) {
memset(in, 0x3f, sizeof(in));
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
for (int i = 0; i < m; i++) {
if (edge[i].u != edge[i].v && edge[i].w < in[edge[i].v]) {
pre[edge[i].v] = edge[i].u; in[edge[i].v] = edge[i].w;
}
}
for (int i = 0; i < n; i++) {
if (i != root && in[i] == INF) { return -1; } //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ͼ
}
int tn = 0; in[root] = 0;
for (int i = 0; i < n; i++) {
res += in[i]; v = i;
while (vis[v] != i && id[v] == -1 && v != root) {
vis[v] = i; v = pre[v];
}
if (v != root && id[v] == -1) {
for (int u = pre[v]; u != v ; u = pre[u]) { id[u] = tn; }
id[v] = tn++;
}
}
if (tn == 0) { break; } //û<><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < n; i++) {
if (id[i] == -1) { id[i] = tn++; }
}
for (int i = 0; i < m;) {
v = edge[i].v; edge[i].u = id[edge[i].u]; edge[i].v = id[edge[i].v];
if (edge[i].u != edge[i].v) { edge[i++].w -= in[v]; }
else { swap(edge[i], edge[--m]); }
}
n = tn; root = id[root];
}
return res;
}
//POJ 3164
int main() {
int C = 0, T, u, v, w;
scanf("%d", &T);
while (++C <= T) {
memset(g, 0x3f, sizeof(g));
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d%d", &u, &v, &w);
if (u == v) { continue; }
g[u][v] = min(g[u][v], w);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (g[i][j] < INF) { edge[m].u = i; edge[m].v = j; edge[m++].w = g[i][j]; }
}
}
wtype ans = Zhuliu(0);
printf("Case #%d: ", C);
if (ans == -1) { puts("Possums!"); }
else { printf("%d\n", ans); }
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>پ<EFBFBD><D9BE><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Kruskal O(VlogV)
const int INF = 0x3f3f3f3f;
struct Point {
int x, y, id;
bool operator<(const Point &r)const { return x < r.x || (x == r.x && y < r.y); }
} p[N];
struct Edge { //<2F><>Ч<EFBFBD><D0A7>
int u, v, w;
bool operator<(const Edge &r)const { return w < r.w; }
} edge[N << 2];
struct BIT { //<2F><>״<EFBFBD><D7B4><EFBFBD><EFBFBD>, <20><>y-x<><78><EFBFBD>ڵ<EFBFBD>ǰ<EFBFBD><C7B0>, <20><><EFBFBD><EFBFBD>y+x<><78>С<EFBFBD><D0A1>
int min_val, pos;
void init() { min_val = INF; pos = -1; }
} bit[N];
int n, tot, fa[N];
int a[N], b[N];
void addedge(int u, int v, int w) { edge[tot].u = u; edge[tot].v = v; edge[tot++].w = w; }
int findfa(int x) { return fa[x] == -1 ? x : fa[x] = findfa(fa[x]); }
inline int cost(Point a, Point b) { return abs(a.x - b.x) + abs(a.y - b.y); }
inline int lowbit(int x) { return x & (-x); }
void update(int i, int val, int pos) {
for (; i > 0; i -= lowbit(i)) {
if (val < bit[i].min_val) { bit[i].min_val = val; bit[i].pos = pos; }
}
}
int query(int i, int m) { //<2F><>ѯ[i, m]<5D><><EFBFBD><EFBFBD>Сֵλ<D6B5><CEBB>
int min_val = INF, pos = -1;
for (; i <= m; i += lowbit(i)) {
if (bit[i].min_val < min_val) { min_val = bit[i].min_val; pos = bit[i].pos; }
}
return pos;
}
void MMST() {
tot = 0;
for (int d = 0; d < 4; d++) { //4<><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (d == 1 || d == 3) { for (int i = 0; i < n; i++) { swap(p[i].x, p[i].y); } }
else if (d == 2) { for (int i = 0; i < n; i++) { p[i].x = -p[i].x; } }
sort(p, p + n);
for (int i = 0; i < n; i++) { a[i] = b[i] = p[i].y - p[i].x; }
sort(b, b + n);
int m = unique(b, b + n) - b;
for (int i = 1; i <= m; i++) { bit[i].init(); }
for (int i = n - 1 ; i >= 0; i--) {
int pos = lower_bound(b, b + m, a[i]) - b + 1, ans = query(pos, m);
if (ans != -1) { addedge(p[i].id, p[ans].id, cost(p[i], p[ans])); }
update(pos, p[i].x + p[i].y, i);
}
}
}
int Kruskal() {
MMST();
memset(fa, -1, sizeof(fa));
sort(edge, edge + tot);
int ret = 0;
for (int i = 0, k = 0; i < tot; i++) {
int u = edge[i].u, v = edge[i].v, t1 = findfa(u), t2 = findfa(v);
if (t1 != t2) {
fa[t1] = t2; ret += edge[i].w;
if (++k == n - 1) { return ret; }
}
}
}
//POJ3241 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>k<EFBFBD><6B><EFBFBD>ı<EFBFBD>
int Kruskal(int k) {
MMST(n, p);
memset(fa, -1, sizeof(fa));
sort(edge, edge + tot);
for (int i = 0; i < tot; i++) {
int u = edge[i].u, v = edge[i].v, t1 = findfa(u), t2 = findfa(v);
if (t1 != t2) {
fa[t1] = t2;
if (--k == 0) { return edge[i].w; }
}
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Matrix-Tree<65><65><EFBFBD><EFBFBD>(Kirchhoff <20><><EFBFBD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
//1<><31>G<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>D[G]<5D><>һ<EFBFBD><D2BB>n*n<>ľ<EFBFBD><C4BE><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>i <20><> jʱ,dij = 0; <20><>i = jʱ, dij<69><6A><EFBFBD><EFBFBD>vi<76>Ķ<EFBFBD><C4B6><EFBFBD>
//2<><32>G<EFBFBD><47><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD>A[G]Ҳ<><D2B2>һ<EFBFBD><D2BB>n*n<>ľ<EFBFBD><C4BE><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD>vi vj֮<6A><D6AE><EFBFBD>б<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><>aij = 1, <20><><EFBFBD><EFBFBD>Ϊ0
//<2F><><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD>G<EFBFBD><47>Kirchhoff<66><66><EFBFBD><EFBFBD><><D2B2>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˹<EFBFBD><CBB9><EFBFBD><EFBFBD>)C[G]ΪC[G] = D[G] - A[G], <20><>Matrix-Tree<65><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>Ϊ: G<><47><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Kirchhoff<66><66><EFBFBD><EFBFBD>C[G]<5D>κ<EFBFBD>һ<EFBFBD><D2BB>n - 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>ֵ
//<2F><>νn - 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ, <20><><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD>r(1 <20><> r <20><> n), <20><>C[G]<5D>ĵ<EFBFBD>r<EFBFBD>С<EFBFBD><D0A1><EFBFBD>r<EFBFBD><72>ͬʱȥ<CAB1><C8A5><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>¾<EFBFBD><C2BE><EFBFBD>, <20><>Cr[G]<5D><>ʾ<EFBFBD><CABE>
============ͼ<><CDBC>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>=========
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//SAP + <20>ڽӾ<DABD><D3BE><EFBFBD> O(V^2*E) <20><><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>Ĭ<EFBFBD>ϴ<EFBFBD>0<EFBFBD><30>ʼ
typedef long long ftype;
int n, dis[N], pre[N], cur[N], gap[N];
ftype cap[N][N];
ftype SAP(int src, int sink, int n) {
memset(dis, 0, sizeof(dis));
memset(cur, 0, sizeof(cur));
memset(gap, 0, sizeof(gap));
int u = src; ftype mxflow = 0, aug = -1; pre[src] = src; gap[0] = n;
while (dis[src] < n) {
loop:
for (int v = cur[u]; v < n; ++v) {
if (cap[u][v] > 0 && dis[u] == dis[v] + 1) {
if (aug == -1 || aug > cap[u][v]) { aug = cap[u][v]; }
pre[v] = u; u = cur[u] = v;
if (v == sink) {
for (u = pre[u]; v != src; v = u, u = pre[u]) {
cap[u][v] -= aug; cap[v][u] += aug;
}
mxflow += aug; aug = -1;
}
goto loop;
}
}
int mndis = n - 1;
for (int v = 0; v < n; v++) {
if (cap[u][v] > 0 && mndis > dis[v]) { cur[u] = v; mndis = dis[v]; }
}
if (--gap[dis[u]] == 0) { break; }
dis[u] = mndis + 1; gap[dis[u]]++; u = pre[u];
}
return mxflow;
}
//ISAP + <20>ڽӱ<DABD> O(V^2*E)
typedef long long ftype;
const ftype INF = 0x3f3f3f3f;
int head[N], to[M], nxt[M], tot, dis[N], pre[N], cur[N], gap[N];
ftype cap[M];
inline void init() { tot = 0; memset(head, -1, sizeof(head)); }
inline void addedge(int x, int y, ftype w, ftype rw = 0) {
to[tot] = y; cap[tot] = w; nxt[tot] = head[x]; head[x] = tot++;
to[tot] = x; cap[tot] = rw; nxt[tot] = head[y]; head[y] = tot++;
}
ftype ISAP(int src, int sink, int n) {
memset(dis, 0, sizeof(dis));
memset(gap, 0, sizeof(gap));
memcpy(cur, head, sizeof(head));
int u = src, v; ftype mxflow = 0; pre[u] = -1; gap[0] = n;
while (dis[src] < n) {
if (u == sink) {
ftype mndis = INF;
for (int i = pre[u]; ~i; i = pre[to[i ^ 1]]) {
if (mndis > cap[i]) { mndis = cap[i]; }
}
for (int i = pre[u]; ~i; i = pre[to[i ^ 1]]) {
cap[i] -= mndis; cap[i ^ 1] += mndis;
}
u = src; mxflow += mndis;
continue;
}
bool flag = false;
for (int i = cur[u]; ~i; i = nxt[i]) {
v = to[i];
if (cap[i] > 0 && dis[v] + 1 == dis[u]) {
flag = true; cur[u] = pre[v] = i; break;
}
}
if (flag) { u = v; continue; }
int mndis = n;
for (int i = head[u]; ~i; i = nxt[i]) {
if (cap[i] > 0 && dis[to[i]] < mndis) { mndis = dis[to[i]]; cur[u] = i; }
}
if (--gap[dis[u]] == 0) { return mxflow; }
dis[u] = mndis + 1; gap[dis[u]]++;
if (u != src) { u = to[pre[u] ^ 1]; }
}
return mxflow;
}
//ISAP + bfs<66><73><EFBFBD><EFBFBD> + queue + <20>ڽӱ<DABD> O(V^2*E)
typedef long long ftype;
const ftype INF = 0x3f3f3f3f;
int n, head[N], to[M], nxt[M], tot, dis[N], pre[N], cur[N], gap[N];
ftype cap[M];
inline void init() { tot = 0; memset(head, -1, sizeof(head)); }
inline void addedge(int x, int y, ftype w, ftype rw = 0) {
to[tot] = y; cap[tot] = w; nxt[tot] = head[x]; head[x] = tot++;
to[tot] = x; cap[tot] = rw; nxt[tot] = head[y]; head[y] = tot++;
}
void bfs(int sink) {
memset(dis, -1, sizeof(dis));
memset(gap, 0, sizeof(gap));
dis[sink] = 0; gap[0] = 1;
queue<int> que; que.push(sink);
while (!que.empty()) {
int u = que.front(); que.pop();
for (int i = head[u], v; ~i; i = nxt[i]) {
v = to[i];
if (~dis[v]) { continue; }
dis[v] = dis[u] + 1; gap[dis[v]]++; que.push(v);
}
}
}
ftype ISAP(int src, int sink, int n) {
bfs(sink);
memcpy(cur, head, sizeof(head));
int u = pre[src] = src, v, i; ftype mxflow = 0;
while (dis[sink] < n) {
if (u == sink) {
ftype mndis = INF; int inser;
for (i = src; i != sink; i = to[cur[i]]) {
if (mndis > cap[cur[i]]) { mndis = cap[cur[i]]; inser = i; }
}
for (i = src; i != sink; i = to[cur[i]]) {
cap[cur[i]] -= mndis; cap[cur[i] ^ 1] += mndis;
}
mxflow += mndis; u = inser;
}
for (i = cur[u]; ~i; i = nxt[i]) {
v = to[i];
if (dis[v] + 1 == dis[u] && cap[i] > 0) { break; }
}
if (~i) { cur[u] = i; pre[to[i]] = u; u = to[i]; }
else {
if (--gap[dis[u]] == 0) { break; }
int mndis = n;
for (i = head[u]; ~i; i = nxt[i]) {
if (cap[i] > 0 && mndis > dis[to[i]]) { cur[u] = i; mndis = dis[to[i]]; }
}
dis[u] = mndis + 1; gap[dis[u]]++; u = pre[u];
}
}
return mxflow;
}
//Dinic O(V^2*E)
typedef long long ftype;
const ftype INF = 0x3f3f3f3f;
int head[N], to[M], nxt[M], tot, dis[N], cur[N], src, sink;
ftype cap[M];
inline void init() { tot = 0; memset(head, -1, sizeof(head)); }
inline void addedge(int x, int y, ftype w, ftype rw = 0) {
to[tot] = y; cap[tot] = w; nxt[tot] = head[x]; head[x] = tot++;
to[tot] = x; cap[tot] = rw; nxt[tot] = head[y]; head[y] = tot++;
}
bool bfs() {
memset(dis, 0, sizeof(dis)); dis[src] = 1;
queue<int> que; que.push(src);
while (!que.empty()) {
int u = que.front(); que.pop();
for (int i = head[u], v; ~i; i = nxt[i]) {
v = to[i];
if (cap[i] > 0 && !dis[v]) { dis[v] = dis[u] + 1; que.push(v); }
}
}
return dis[sink];
}
ftype dfs(int u, ftype delta) {
if (u == sink || delta == 0) { return delta; }
ftype ret = 0;
for (int &i = cur[u], v; delta && ~i; i = nxt[i]) {
v = to[i];
if (cap[i] > 0 && dis[v] == dis[u] + 1) {
ftype aug = dfs(v, min(cap[i], delta));
if (!aug) { continue; }
cap[i] -= aug; cap[i ^ 1] += aug; delta -= aug; ret += aug;
if (!delta) { break; }
}
}
return ret;
}
ftype Dinic() {
ftype ret = 0;
while (bfs()) {
memcpy(cur, head, sizeof(head));
ret += dfs(src, INF);
}
return ret;
}
//HLPP Highest Label Preflow Push O(V^3)
typedef long long ftype;
struct Edge { int v; ftype c; } edge[M];
vector<int> e[N];
int n, m, src, sink, c[N << 1], d[N], done[N];
ftype w[N];
bool vis[N];
void init(int _n, int _src, int _sink) {
n = _n; m = 0; src = _src; sink = _sink;
for (int i = 0; i <= n; ++i) { e[i].clear(); }
}
inline void addEdge(int x, int y, ftype w, ftype rw = 0) {
edge[m].v = y; edge[m].c = w; e[x].push_back(m++);
edge[m].v = x; edge[m].c = rw; e[y].push_back(m++);
}
void bfs() {
memset(c, 0, sizeof(c));
fill(d, d + n, n + 1);
queue<int> que; que.push(sink);
c[n + 1] = n - 1; d[src] = n; d[sink] = 0;
while (!que.empty()) {
int u = que.front(); que.pop();
c[n + 1]++; c[d[u]]++;
for (int i = 0, v; i < (int)e[u].size(); i++) {
v = edge[e[u][i]].v; ftype c = edge[e[u][i] ^ 1].c;
if (d[v] == n + 1 && c > 0) { d[v] = d[u] + 1; que.push(v); }
}
}
}
int HLPP(int n) {
memset(w, 0, sizeof(w));
memset(done, 0, sizeof(done));
memset(vis, 0, sizeof(vis));
bfs();
int todo = -1;
vector<queue<int>> que(n << 1);
vis[src] = vis[sink] = true;
for (int i = 0, v; i < (int)e[src].size(); i++) {
Edge &arc = edge[e[src][i]], &cra = edge[e[src][i] ^ 1]; v = arc.v;
w[v] += arc.c; cra.c += arc.c; arc.c = 0;
if (!vis[v]) { vis[v] = true; que[d[v]].push(v); todo = max(todo, d[v]); }
}
while (todo >= 0) {
if (que[todo].empty()) { todo--; continue; }
int u = que[todo].front(); que[todo].pop();
vis[u] = false;
while (done[u] < (int)e[u].size()) {
Edge &arc = edge[e[u][done[u]]]; int v = arc.v;
if (d[u] == d[v] + 1 && arc.c > 0) {
Edge &cra = edge[e[u][done[u]] ^ 1]; ftype f = min(w[u], arc.c);
w[u] -= f; w[v] += f; arc.c -= f; cra.c += f;
if (!vis[v]) { vis[v] = true; que[d[v]].push(v); }
if (w[u] == 0) { break; }
}
done[u]++;
}
if (w[u] > 0) {
int du = d[u];
c[d[u]]--; d[u] = n << 1;
for (int i = 0, v; i < (int)e[u].size(); i++) {
Edge &arc = edge[e[u][i]]; v = arc.v;
if (d[u] > d[v] + 1 && arc.c > 0) { d[u] = d[v] + 1; done[u] = i; }
}
c[d[u]]++;
if (c[du] == 0) {
for (int i = 0; i < n; ++i) {
if (d[i] > du && d[i] < n + 1) { c[d[i]]--; c[n + 1]++; d[i] = n + 1; }
}
}
vis[u] = true; que[d[u]].push(u); todo = d[u];
}
}
return w[sink];
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>, <20>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊԭ<CEAA><D4AD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD>ȥ<EFBFBD>½<EFBFBD><C2BD>IJ<EFBFBD>, <20><>¼ÿ<C2BC><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>-<2D><><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>
//<2F><><EFBFBD>½<EFBFBD><C2BD>ʹ<EFBFBD><CDB4><EFBFBD><30><CAB1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><E3B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>, <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>
//<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD>С<EFBFBD><D0A1><30><CAB1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>, <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>½<EFBFBD><C2BD>͵ľ<CDB5><C4BE><EFBFBD>ֵ
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>Դ<EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>򲻴<EFBFBD><F2B2BBB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F>ӻ<EFBFBD><D3BB><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>INF<4E>ıߵ<C4B1>Դ, <20><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>Դ<EFBFBD><D4B4>
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><E3BCB0><EFBFBD><EFBFBD>(head[]ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨ʱ, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>)
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ԭԴ<D4AD><D4B4>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD>С<EFBFBD><D0A1>
//<2F><><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>, <20><>һ<EFBFBD><EFBFBD><E9B3AC>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>INF<4E>ıߵ<C4B1>ԭԴ<D4AD><D4B4>, <20><><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><E9B3AC>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>п<EFBFBD><D0BF>н<EFBFBD>, <20><>Ϊԭ<CEAA><D4AD><EFBFBD>㵽ԭԴ<D4AD><D4B4><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>
////<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MCMF O(V*E*f)
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>dis[sink]Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><>dis[sink]<5D><>ΪINF<4E><46><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ȡ<EFBFBD><EFBFBD><E0B7B4>, <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><E0B7B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
typedef long long ftype;
const ftype INF = 0x3f3f3f3f;
int head[N], to[M], nxt[M], tot, cur[N];
ftype cap[M], cost[M], flow[N], dis[N], mncost, mxflow;
bool vis[N];
inline void init() { tot = 0; memset(head, -1, sizeof(head)); }
inline void addedge(int x, int y, ftype w, ftype c) {
to[tot] = y; cap[tot] = w; cost[tot] = c; nxt[tot] = head[x]; head[x] = tot++;
to[tot] = x; cap[tot] = 0; cost[tot] = -c; nxt[tot] = head[y]; head[y] = tot++;
}
bool SPFA(int src, int sink) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[src] = 0; cur[src] = -1; flow[src] = INF;
queue<int> que; que.push(src);
while (!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for (int i = head[u], v; ~i; i = nxt[i]) {
v = to[i];
if (cap[i] > 0 && dis[v] > dis[u] + cost[i]) {
dis[v] = dis[u] + cost[i]; flow[v] = min(flow[u], cap[i]); cur[v] = i;
if (!vis[v]) { vis[v] = true; que.push(v); }
}
}
}
if (dis[sink] == INF) { return false; }
mxflow += flow[sink]; mncost += flow[sink] * dis[sink];
for (int i = cur[sink]; ~i; i = cur[to[i ^ 1]]) {
cap[i] -= flow[sink]; cap[i ^ 1] += flow[sink];
}
return true;
}
ftype MCMF(int src, int sink) {
mxflow = mncost = 0;
while (SPFA(src, sink));
return mncost;
}
===========ͼ<><CDBC><><D3A6>==========
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(V^2)
//queue-><3E><>ͨ<EFBFBD>ж<EFBFBD><D0B6><EFBFBD>, priority_queue-><3E>ֵ<EFBFBD><D6B5><EFBFBD>
//<2F>ڽӾ<DABD><D3BE><EFBFBD>
int n, mp[N][N], in[N], ret[N];
int topoSort() {
queue<int> que; int k = 0; bool flag = false;
for (int i = 0; i < n; i++) { if (in[i] == 0) { que.push(i); } }
while (!que.empty()) {
if (que.size() > 1) { flag = true; }
int u = que.front(); que.pop(); ret[k++] = u;
for (int v = 0; v < n; v++) {
if (mp[u][v] && --in[v] == 0) { que.push(v); }
}
}
return k < n ? -1 : flag ? 0 : 1; //<2F>л<EFBFBD>, <20><>Ψһ, Ψһ
}
//<2F>ڽӱ<DABD>
int head[N], to[M], nxt[M], tot, n, in[N], ret[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
bool topoSort() {
priority_queue<int> que; int k = 0;
for (int i = 0; i < n; i++) { if (in[i] == 0) { que.push(i); } }
while (!que.empty()) {
int u = que.top(); que.pop(); ret[k++] = u;
for (int i = head[u]; ~i; i = nxt[i]) {
if (--in[to[i]] == 0) { que.push(to[i]); }
}
}
return k == n;
}
//Ⱦɫ<C8BE>ж϶<D0B6><CFB6><EFBFBD>ͼ
bool col[N];
bool Color(int u) {
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!col[v]) {
col[v] = !col[u];
if (!Color(v)) { return false; }
} else if (col[v] == col[u]) { return false; }
}
return true;
}
//ŷ<><C5B7><EFBFBD><EF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D><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD>ȶ<EFBFBD>1, <20><><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ͼ: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD>·, һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŷ<EFBFBD><C5B7>·<EFBFBD><C2B7><EFBFBD>ˡ<EFBFBD><CBA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD> - <20><><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
//<2F><>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD>, <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ŷ<EFBFBD><C5B7><EFB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- <20><>i<EFBFBD><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>), <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>D[i]Ϊ<><CEAA><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ͨ, <20><><EFBFBD>޽<EFBFBD>
//<2F><><EFBFBD><EFBFBD>ʼ<44><D6B5><EFBFBD><EFBFBD>ż<EFBFBD><C5BC>, <20><><EFBFBD><EFBFBD>G'<27><>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>S<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>T
//<2F><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>D[i] > 0<>ĵ<EFBFBD>i, <20><><EFBFBD><EFBFBD><S, i>, <20><><EFBFBD><EFBFBD>ΪD[i] / 2; <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>D[j] < 0<>ĵ<EFBFBD>j, <20><><EFBFBD><EFBFBD><j, T>, <20><><EFBFBD><EFBFBD>Ϊ-D[j] / 2
//G'<27>е<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>, <20><><EFBFBD><EFBFBD>Ϊ1, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б߾<D0B1><DFBE><EFBFBD><EFBFBD><EFBFBD>, <20><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>ŷ<EFBFBD><C5B7>ͼ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1<CEAA>IJ<EFBFBD><C4B2><EFBFBD>S<EFBFBD><53>T<EFBFBD><54><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>G'<27>иı<C4B1><E4B7BD>,<2C>γɵ<CEB3><C9B5><EFBFBD>ͼG''һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŷ<EFBFBD><C5B7>ͼ
//ŷ<><C5B7><EFBFBD><EFBFBD>· + <20>ڽӾ<DABD><D3BE><EFBFBD> O(N^2)
//<2F><>ŷ<EFBFBD><C5B7>·<EFBFBD><C2B7>/<2F><>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD> ֧<><D6A7><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD>ر<EFBFBD>
int n, mp[N][N], path[N], cnt;
void dfsu(int u) {
for (int v = n - 1; v >= 0; v--) {
while (mp[u][v]) { mp[u][v]--; mp[v][u]--; dfsu(v); }
}
path[cnt++] = u;
}
void dfsd(int u) {
for (int v = n - 1; v >= 0; v--) {
while (mp[u][v]) { mp[u][v]--; dfsd(v); }
}
path[cnt++] = u;
}
//<2F><><EFBFBD><EFBFBD>ͼ SGU101
int head[N], to[M], nxt[M], tot, deg[N], path[M], cnt;
bool vis[M];
void init() {
tot = 0; cnt = 0;
memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(vis)); memset(deg, 0, sizeof(deg));
}
void addedge(int x, int y) {
to[tot] = y; nxt[tot] = head[x]; head[x] = tot++;
to[tot] = x; nxt[tot] = head[y]; head[y] = tot++;
}
void dfs(int u) {
for (int &i = head[u]; ~i;) {
if (!vis[i]) { vis[i] = vis[i ^ 1] = true; int t = i; dfs(to[i]); path[cnt++] = t; }
else { i = nxt[i]; }
}
}
int main() {
int n, u, v;
while (~scanf("%d", &n)) {
init();
for (int i = 0; i < n; i++) {
scanf("%d%d", &u, &v); addedge(u, v); deg[u]++; deg[v]++;
}
int s = -1, cnto = 0;
for (int i = 0; i <= 6; i++) {
if (s == -1 && deg[i] > 0) { s = i; }
if (deg[i] & 1) { cnto++; s = i; }
}
if (cnto != 0 && cnto != 2) { puts("No solution"); continue; }
dfs(s);
if (cnt != n) { puts("No solution"); continue; }
for (int i = 0; i < cnt; i++) {
printf("%d %c\n", (path[i] >> 1) + 1, path[i] & 1 ? '+' : '-');
}
}
}
//<2F><><EFBFBD><EFBFBD>ͼ POJ2337
//<2F><><EFBFBD><EFBFBD>n<EFBFBD><6E>Сд<D0A1><D0B4>ĸ<EFBFBD><C4B8><EFBFBD>ɵĵ<C9B5><C4B5><EFBFBD>, Ҫ<><D2AA><EFBFBD><EFBFBD>n<EFBFBD><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>Ľ<EFBFBD>
int head[N], to[M], nxt[M], tot, in[N], out[N], path[N], cnt;
bool vis[M];
string str[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) {
to[tot] = y; nxt[tot] = head[x]; head[x] = tot++;
}
void dfs(int u) {
for (int &i = head[u]; ~i;) {
if (!vis[i]) { vis[i] = true; int t = i; dfs(to[i]); path[cnt++] = n - t - 1; }
else { i = nxt[i]; }
}
}
int main() {
int T, n;
char s[25];
scanf("%d", &T);
while (T--) {
init(); cnt = 0;
memset(vis, 0, sizeof(vis));
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
scanf("%d", &n);
for (int i = 0; i < n; i++) { scanf("%s", s); str[i] = s; }
sort(str, str + n);
int s = 26;
for (int i = n - 1; i >= 0; i--) { //<2F>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
int u = str[i][0] - 'a', v = str[i][str[i].size() - 1] - 'a';
addedge(u, v); out[u]++; in[v]++;
s = min(s, min(u, v));
}
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < 26; i++) {
if (out[i] - in[i] == 1) { cnt1++; s = i; } //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD>ȴ<EFBFBD>1<EFBFBD>ĵ<EFBFBD>, <20>ʹ<EFBFBD><CDB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>
else if (out[i] - in[i] == -1) { cnt2++; }
else if (out[i] - in[i] != 0) { cnt1 = 3; }
}
if (!((cnt1 == 0 && cnt2 == 0) || (cnt1 == 1 && cnt2 == 1))) { puts("***"); continue; }
dfs(s);
if (cnt != n) { puts("***"); continue; }
for (int i = cnt - 1; i >= 0; i--) {
printf("%s%c", str[path[i]].c_str(), i > 0 ? '.' : '\n');
}
}
}
//<2F><><EFBFBD><EFBFBD>ͼ <20><><EFBFBD>鼯 HDU1116
//<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ܲ<EFBFBD><DCB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>, <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>ĵ<EFBFBD><C4B5><EFBFBD>, Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int n, fa[N], in[N], out[N], p[N];
bool vis[N];
char s[M];
void init() {
memset(vis, 0, sizeof(vis)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out));
for (int i = 0; i < N; i++) { fa[i] = i; }
}
int findfa(int n) {
return n == fa[n] ? n : fa[n] = findfa(fa[n]);
}
inline void unite(int x, int y) {
x = findfa(x); y = findfa(y);
if (x != y) { fa[y] = x; }
}
int main() {
int T, n;
scanf("%d", &T);
while (T--) {
init();
scanf("%d", &n);
while (n--) {
scanf("%s", s); int u = s[0] - 'a', v = s[strlen(s) - 1] - 'a';
unite(u, v); out[u]++; in[v]++; vis[u] = vis[v] = true;
}
int cnt = 0, k = 0;
for (int i = 0; i < N; i++) {
fa[i] = findfa(i);
if (vis[i] && fa[i] == i) { cnt++; }
}
if (cnt > 1) { puts("The door cannot be opened."); continue; } //<2F><><EFBFBD><EFBFBD>ͨ
for (int i = 0; i < N; i++) {
if (in[i] != out[i]) { p[k++] = i; if (k > 2) { break; } }
}
if (k == 0 || (k == 2 && ((out[p[0]] - in[p[0]] == 1 && in[p[1]] - out[p[1]] == 1) ||
(out[p[1]] - in[p[1]] == 1 && in[p[0]] - out[p[0]] == 1)))) {
puts("Ordering is possible.");
} else { puts("The door cannot be opened."); }
}
}
//<2F><><EFBFBD><EFBFBD>ͼ POJ1637 (<28><><EFBFBD>Ᵽ֤<E2B1A3><D6A4>ͨ)
//<2F>ж<EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD>· <20><>ISAP + <20>ڽӱ<DABD> O(V^2*E)
int in[N], out[N];
int main() {
int T, u, v, w;
scanf("%d", &T);
while (T--) {
init();
memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out));
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d%d", &u, &v, &w); out[u]++; in[v]++;
if (w == 0) { addedge(u, v, 1); } //˫<><CBAB>
}
bool flag = true;
for (int i = 1; i <= n; i++) {
if (out[i] - in[i] > 0) { addedge(0, i, (out[i] - in[i]) >> 1); }
else if (in[i] - out[i] > 0) { addedge(i, n + 1, (in[i] - out[i]) >> 1); }
if (out[i] - in[i] & 1) { flag = false; break; }
}
if (!flag) { puts("impossible"); continue; }
ISAP(0, n + 1, n + 2);
for (int i = head[0]; ~i; i = nxt[i]) {
if (cap[i] > 0 && cap[i] > flow[i]) { flag = false; break; }
}
puts(flag ? "possible" : "impossible");
}
}
//2-SAT
//Ⱦɫ<C8BE><C9AB>
const int N = 20005;
const int M = 100005;
int head[N], to[M], nxt[M], tot;
bool vis[N]; //Ⱦɫ<C8BE><C9AB><EFBFBD><EFBFBD>
int S[N], top; //ջ
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
bool dfs(int u) {
if (vis[u ^ 1]) { return false; }
if (vis[u]) { return true; }
vis[u] = true; S[top++] = u;
for (int i = head[u]; ~i; i = nxt[i]) {
if (!dfs(to[i])) { return false; }
}
return true;
}
bool twoSAT(int n) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i += 2) {
if (vis[i] || vis[i ^ 1]) { continue; }
top = 0;
if (!dfs(i)) {
while (top) { vis[S[--top]] = false; }
if (!dfs(i ^ 1)) { return false; }
}
}
return true;
}
//HDU 1814
int main() {
int n, m, u, v;
while (~scanf("%d%d", &n, &m)) {
init();
while (m--) {
scanf("%d%d", &u, &v); u--; v--;
addedge(u, v ^ 1); addedge(v, u ^ 1);
}
if (twoSAT(n << 1)) {
for (int i = 0; i < n << 1; i++) {
if (vis[i]) { printf("%d\n", i + 1); }
}
} else { printf("NIE\n"); }
}
}
//Tarjanǿ<6E><C7BF>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
const int N = 1005;
const int M = 100005;
int head[N], to[M], nxt[M], tot;
int num[N], Low[N], DFN[N], S[N], Belong[N], idx, top, scc; //Belong<6E><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ1~scc
bool instack[N];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y) { to[tot] = y; nxt[tot] = head[x]; head[x] = tot++; }
void Tarjan(int u) {
Low[u] = DFN[u] = ++idx; S[top++] = u; instack[u] = true;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!DFN[v]) { Tarjan(v); Low[u] = min(Low[u], Low[v]); }
else if (instack[v] && Low[u] > DFN[v]) { Low[u] = DFN[v]; }
}
if (Low[u] == DFN[u]) {
scc++;
do { v = S[--top]; instack[v] = false; Belong[v] = scc; num[scc]++; } while (v != u);
}
}
bool solvable(int n) { //n<><6E><EFBFBD>ܸ<EFBFBD><DCB8><EFBFBD>, <20><>Ҫѡ<D2AA><D1A1>һ<EFBFBD><D2BB>
memset(DFN, 0, sizeof(DFN));
memset(instack, 0, sizeof(instack));
memset(num, 0, sizeof(num));
idx = scc = top = 0;
for (int i = 0; i < n; i++) { if (!DFN[i]) { Tarjan(i); } }
for (int i = 0; i < n; i += 2) { if (Belong[i] == Belong[i ^ 1]) { return false; } }
return true;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ⲿ<EFBFBD><E2B2BF>
queue<int> q1, q2;
vector<vector<int>> dag; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DAGͼ
char color[N]; //Ⱦɫ, Ϊ'R'<27><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>
int cf[N], indeg[N]; //<2F><><EFBFBD><EFBFBD>
void solve(int n) {
dag.assign(scc + 1, vector<int>());
memset(indeg, 0, sizeof(indeg));
memset(color, 0, sizeof(color));
for (int u = 0; u < n; u++) {
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (Belong[u] != Belong[v]) { dag[Belong[v]].push_back(Belong[u]); indeg[Belong[u]]++; }
}
}
for (int i = 0; i < n; i += 2) {
cf[Belong[i]] = Belong[i ^ 1]; cf[Belong[i ^ 1]] = Belong[i];
}
while (!q1.empty()) { q1.pop(); }
while (!q2.empty()) { q2.pop(); }
for (int i = 1; i <= scc; i++) { if (indeg[i] == 0) { q1.push(i); } }
while (!q1.empty()) {
int u = q1.front(); q1.pop();
if (color[u] == 0) { color[u] = 'R'; color[cf[u]] = 'B'; }
for (int i = 0; i < (int)dag[u].size(); i++) {
if (--indeg[dag[u][i]] == 0) { q1.push(dag[u][i]); }
}
}
}
int change(char s[]) {
int ret = 0, i = 0;
while (s[i] >= '0' && s[i] <= '9') { ret *= 10; ret += s[i++] - '0'; }
return (ret << 1) + (s[i] != 'w');
}
//POJ3648
int main() {
int n, m, u, v;
char s1[10], s2[10];
while (scanf("%d%d", &n, &m), (n || m)) {
init();
while (m--) {
scanf("%s%s", s1, s2); u = change(s1); v = change(s2);
addedge(u ^ 1, v); addedge(v ^ 1, u);
}
addedge(1, 0);
if (solvable(n << 1)) {
solve(n << 1);
for (int i = 1; i < n; i++) {
//ע<><D7A2><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>color[Belong[
if (color[Belong[i << 1]] == 'R') { printf("%dw", i); }
else { printf("%dh", i); }
putchar(i != n - 1 ? ' ' : '\n');
}
} else { printf("bad luck\n"); }
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD> O(n*2^n)
int mp[N][N], stk[N][N], dp[N], ans;
bool dfs(int crt, int tot) {
if (!crt) {
if (tot > ans) { ans = tot; return true; }
return false;
}
for (int i = 0, u, nxt; i < crt; i++) {
u = stk[tot][i]; nxt = 0;
if (crt - i + tot <= ans) { return false; }
if (dp[u] + tot <= ans) { return false; }
for (int j = i + 1; j < crt; j++) {
int v = stk[tot][j];
if (g[u][v]) { stk[tot + 1][nxt++] = v; }
}
if (dfs(nxt, tot + 1)) { return true; }
}
return false;
}
int maxClique(int n) {
ans = 0;
for (int i = n - 1, j, k; i >= 0; i--) {
for (j = i + 1, k = 0; j < n; j++) {
if (g[i][j]) { stk[1][k++] = j; }
}
dfs(k, 1); dp[i] = ans;
}
return ans;
}
//<2F><><EFBFBD><EFBFBD>̰<EFBFBD><CCB0> O(T*n^2)
const int T = 1000;
int mp[N][N], id[N], ansn, ans[N]; bool del[N];
void solve(int n) {
memset(del, 0, sizeof(del)); int k = 0;
for (int i = 0, j; i < n; i++) {
if (del[i]) { continue; }
for (j = i + 1, k++; j < n; j++) { if (!mp[id[i]][id[j]]) { del[j] = true; } }
}
if (k > ansn) {
ansn = k;
for (int i = k = 0; i < n; i++) { if (!del[i]) { ans[k++] = id[i]; } }
}
}
void maxClique(int n) {
for (int i = 0; i < n; i++) { id[i] = i; }
for (int t = 0; t < T; t++) {
for (int i = 0; i < n; i++) { swap(id[i], id[rand() % n]); } solve();
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>㷨 O(T*(V+E))
const int T = 1000;
int q[N], pos[N]; bool del[N];
int solve(int n) {
int ans = 0;
for (int t = 0; t < T; t++) {
int ret = 0, top = n; memset(del, 0, sizeof(del));
for (int i = 1; i <= n; i++) { q[i] = pos[i] = i; }
while (top) {
int x = rand() % top + 1, u = q[x]; q[x] = q[top--]; pos[q[x]] = x; ret++;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!del[v]) { del[v] = true; x = pos[v]; q[x] = q[top--]; pos[q[x]] = x; }
}
}
ans = max(ans, ret);
}
return ans;
}
==========ͼ<><CDBC>-<2D><><EFBFBD><EFBFBD>·=========
//<2F>ڽӱ<DABD>
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
int head[N], to[M], nxt[M], tot; wtype len[M];
void init() { tot = 0; memset(head, -1, sizeof(head)); }
void addedge(int x, int y, wtype z) { to[tot] = y; len[tot] = z; nxt[tot] = head[x]; head[x] = tot++; }
//Dijkstra + priority_queue + <20>ڽӾ<DABD><D3BE><EFBFBD> O(V^2)
typedef int wtype;
struct Node {
int v; wtype w;
bool operator<(const Node &r)const { return w > r.w; }
};
int n, pre[N]; wtype mp[N][N], dist[N]; bool vis[N]; //<2F>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ΪINF
void Dijkstra(int src) {
memset(dist, 0x3f, sizeof(dist));
memset(vis, 0, sizeof(vis)); memset(pre, -1, sizeof(pre));
priority_queue<Node> que; que.push((Node) {src, dist[src] = 0});
while (!que.empty()) {
int u = que.top().v; que.pop();
if (vis[u]) { continue; }
vis[u] = true;
for (int v = 0; v < n; v++) {
if (!vis[v] && dist[v] > dist[u] + mp[u][v]) {
dist[v] = dist[u] + mp[u][v]; pre[v] = u; que.push((Node) {v, dist[v]});
}
}
}
}
//Dijkstra + priority_queue + vector<6F><72><EFBFBD><EFBFBD> O((V+E)logV)
typedef int wtype;
struct Node {
int v; wtype w;
bool operator<(const Node &r)const { return w > r.w; }
};
vector<Node> e[N]; wtype dist[N]; bool vis[N];
void Dijkstra(int src) {
memset(dist, 0x3f, sizeof(dist));
memset(vis, 0, sizeof(vis));
priority_queue<Node> que; que.push((Node) {src, dist[src] = 0});
while (!que.empty()) {
int u = que.top().v; que.pop();
if (vis[u]) { continue; }
vis[u] = true;
for (int i = 0; i < (int)e[u].size(); i++) {
int v = e[u][i].v; wtype w = e[u][i].w;
if (!vis[v] && dist[v] > dist[u] + w) {
dist[v] = dist[u] + w; que.push((Node) {v, dist[v]});
}
}
}
}
//Dijkstra + priority_queue + <20>ڽӱ<DABD> O((V+E)logV)
typedef int wtype;
struct Node {
int v; wtype w;
bool operator<(const Node &r)const { return w > r.w; }
};
wtype dist[N];
void Dijkstra(int src) {
memset(dist, 0x3f, sizeof(dist));
priority_queue<Node> que; que.push((Node) {src, dist[src] = 0});
while (!que.empty()) {
int u = que.top().v; wtype w = que.top().w; que.pop();
if (w > dist[u]) { continue; }
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (dist[v] > len[i] + w) {
dist[v] = w + len[i]; que.push((Node) {v, dist[v]});
}
}
}
}
//SPFA + queue/stack + vector<6F><72><EFBFBD><EFBFBD> O(kE)
typedef int wtype;
struct Edge { int v; wtype w; };
vector<Edge> e[N]; int cnt[N], pre[N]; wtype dist[N]; bool vis[N];
bool SPFA(int src) {
memset(dist, 0x3f, sizeof(dist)); dist[src] = 0;
memset(cnt, 0, sizeof(cnt)); cnt[src] = 1;
memset(vis, 0, sizeof(vis)); vis[src] = true;
memset(pre, -1, sizeof(pre));
queue<int> que; que.push(src); //stack
while (!que.empty()) {
int u = que.front(); que.pop();
vis[u] = false;
for (int i = 0; i < (int)e[u].size(); i++) {
int v = e[u][i].v; wtype w = e[u][i].w;
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w; pre[v] = u;
if (!vis[v]) {
vis[v] = true; que.push(v);
if (++cnt[v] > n) { return false; } //<2F>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>·
}
}
}
}
return true; //û<>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>·
}
//SPFA + SLF + LLL + <20>ڽӱ<DABD> O(kE)
typedef int wtype;
wtype dist[N];
bool vis[M];
void SPFA(int src) {
memset(dist, 0x3f, sizeof(dist)); dist[src] = 0;
memset(vis, 0, sizeof(vis)); vis[src] = true;
deque<int> que; que.push_back(src);
ll sum = 0;
while (!que.empty()) {
int u = que.front(); que.pop_front();
if (!que.empty() && (ll)dist[u] * que.size() > sum) { que.push_back(u); continue; }
sum -= dist[u]; vis[u] = false;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i]; wtype d = dist[u] + len[i];
if (d < dist[v]) {
if (vis[v]) { sum += d - dist[v]; }
dist[v] = d;
if (!vis[v]) {
if (!que.empty() && dist[que.front()] > dist[v]) { que.push_front(v); }
else { que.push_back(v); }
sum += dist[v]; vis[v] = true;
}
}
}
}
}
//<2F><><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>ϵͳ
//a<><61>b<EFBFBD><62>һ<EFBFBD><D2BB>ȨֵΪc<CEAA><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߱<EFBFBD>ʾb ? a <= c, <20><>SPFA<46>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD>, <20><><EFBFBD>ڼ<EFBFBD><DABC>޽<EFBFBD>
//Bellman-Ford + vector O(VE)
//<2F><><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩͼ, <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD>·, <20><><EFBFBD>ҽ<EFBFBD><D2BD><EFBFBD>ͼ<EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ɴ<EFBFBD><C9B4>ĸ<EFBFBD>Ȩ<EFBFBD><C8A8>·ʱ<C2B7><CAB1><EFBFBD><EFBFBD>true
typedef int wtype;
struct Edge { int u, v; wtype w; };
vector<Edge> e;
wtype dist[N];
bool BellmanFord(int src) {
memset(dist, 0x3f, sizeof(dist)); dist[src] = 0;
for (int i = 1; i < n; i++) {
bool flag = false;
for (int j = 0; j < (int)e.size(); j++) {
int u = e[j].u, v = e[j].v; wtype w = e[j].w;
if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; flag = true; }
}
if (!flag) { return true; } //û<>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>·
}
for (int j = 0; j < (int)e.size(); j++) {
int u = e[j].u, v = e[j].v; wtype w = e[j].w;
if (dist[v] > dist[u] + w) { return false; } //<2F>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>·
}
return true; //û<>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>·
}
//Floyd <20><>·<EFBFBD><C2B7><EFBFBD><EFBFBD>¼ O(V^3)
typedef int wtype;
int n, pre[N][N]; wtype mp[N][N]; //<2F><>ʼ<EFBFBD><CABC>ΪINF
void Floyd() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) { pre[i][j] = (i == j) ? -1 : i; }
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (mp[i][k] + mp[k][j] < mp[i][j]) {
mp[i][j] = mp[i][k] + mp[k][j]; pre[i][j] = pre[k][j];
}
}
}
}
}
//<2F>޸<EFBFBD>Ȩͼ<C8A8><CDBC><EFBFBD><EFBFBD>С<EFBFBD><D0A1>
//<2F><><EFBFBD><EFBFBD>ͼ: mp[i][i] = INF, Ȼ<><C8BB><EFBFBD><EFBFBD>floyd, ans = min(d[i][i])
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>о<EFBFBD><D0BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>:
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
int n; wtype mp[N][N], d[N][N]; //<2F><>ʼ<EFBFBD><CABC>ΪINF
wtype cycFloyd() {
memcpy(d, mp, sizeof(mp)); wtype ret = INF;
for (int k = 1; k <= n; k++) {
for (int i = 1; i < k; i++) {
for (int j = i + 1; j < k; j++) {
ans = min(ans, d[i][j] + mp[i][k] + mp[k][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
//Astar<61><72>k<EFBFBD><6B>·
typedef int wtype;
const wtype INF = 0x3f3f3f3f;
int head[N], to[M], nxt[M], tot; wtype len[M];
int rhead[N], rto[M], rnxt[M], rtot; wtype rlen[M];
void init() { tot = rtot = 0; memset(head, -1, sizeof(head)); memset(rhead, -1, sizeof(rhead)); }
void addedge(int x, int y, wtype z) {
to[tot] = y; len[tot] = z; nxt[tot] = head[x]; head[x] = tot++;
rto[rtot] = x; rlen[rtot] = z; rnxt[rtot] = rhead[y]; rhead[y] = rtot++;
}
struct Node {
int v; wtype w;
bool operator<(const Node &r)const { return w > r.w; }
};
int vis[N]; wtype dist[N], ans[N]; //ǰk<C7B0><6B>·
void Astar(int src, int des, int k) {
memset(dist, 0x3f, sizeof(dist)); memset(ans, 0x3f, sizeof(ans)); memset(vis, 0, sizeof(vis));
priority_queue<Node> que; que.push((Node) {des, dist[des] = 0});
while (!que.empty()) {
int u = que.top().v; wtype w = que.top().w; que.pop();
if (w > dist[u]) { continue; }
for (int i = rhead[u]; ~i; i = rnxt[i]) {
int v = rto[i];
if (dist[v] > rlen[i] + w) {
dist[v] = w + rlen[i]; que.push((Node) {v, dist[v]});
}
}
}
if (dist[src] < INF) { que.push((Node) {src, dis[src]}); }
while (!que.empty()) {
int u = que.top().v; wtype w = que.top().w; que.pop();
if (u == des && vis[des] <= k) { ans[vis[des]] = w; }
if (vis[u] > k) { continue; }
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i]; que.push((Node) {v, w - dist[u] + dist[v] + len[i]});
}
}
}
===========Tarjan <20><EFBFBD><E3B7A8>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
int dfs[MAXN], low[MAXN], tim = 0;
inline void Dfs(int u) {
dfs[u] = low[u] = ++tim;
for (Edge *p = a[u]; p; p = p->next) if (p->flag) {
if (!dfs[p->y]) {
p->opt->flag = false;
Dfs(p->y);
low[u] = min(low[u], low[p->y]);
} else low[u] = min(low[u], dfs[p->y]);
}
for (Edge *p = a[u]; p; p = p->next) if (p->opt->flag == false && low[p->y] > dfs[u]) {
p->bridge = p->opt->bridge = true;
}
}
=========Hopcroft <20><EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ߵ<EFBFBD><DFB5>ı<EFBFBD><C4B1><EFBFBD>Ϊ1-n<><6E><EFBFBD>ұߵ<D2B1><DFB5><EFBFBD><EFBFBD><EFBFBD>Ϊn+1 - N<><4E>match[i]<5D><>ʾ<EFBFBD><CABE><EFBFBD>ߵ<EFBFBD><69><C6A5><EFBFBD>ϵ<EFBFBD><CFB5>ұߵĵ㣬0<E3A3AC><30>ƥ<EFBFBD>䣬link[i]<5D><>ʾ<EFBFBD>ұߵ<D2B1><69><C6A5><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>ߵĵ㣬0<E3A3AC><30>ƥ<EFBFBD><C6A5>
const int MAXN = 10000;
int match[MAXN];
int levelx[MAXN], levely[MAXN], link[MAXN];
int d[MAXN];
inline bool Bfs(void) {
int head = 1, tail = 0;
memset(levely, 0, sizeof levely);
for (int i = 1; i <= n; i++) {
if (!match[i]) d[++tail] = i;
levelx[i] = 0;
}
bool ret = false;
while (head <= tail) {
int now = d[head++];
for (Edge *p = a[now]; p; p = p->next) if (levely[p->y] == 0) {
levely[p->y] = levelx[now] + 1;
if (link[p->y] == 0) ret = true;
else levelx[link[p->y]] = levely[p->y] + 1, d[++tail] = link[p->y];
}
}
return ret;
}
bool Find(int u) {
for (Edge *p = a[u]; p; p = p->next) if (levely[p->y] == levelx[u] + 1) {
levely[p->y] = 0;
if (link[p->y] == 0 || Find(link[p->y])) {
match[u] = p->y; link[p->y] = u;
return true;
}
}
return false;
}
inline void Match(void) {
while (Bfs())
for (int i = 1; i <= n; i++)
if (!match[i]) Find(i);
}
inline void clear(void) {
memset(match, 0, sizeof match);
memset(link, 0, sizeof link);
memset(levelx, 0, sizeof levelx);
memset(levely, 0, sizeof levely);
}
=============KM<4B><EFBFBD><E3B7A8><EFBFBD><EFBFBD>СȨ<D0A1><C8A8><EFBFBD><EFBFBD>ƥ<EFBFBD>䣨ע<E4A3A8><EFBFBD><E2B2BB>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩƥ<C8A8><EFBFBD><E4A3A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB>߷<EFBFBD><DFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȨֵΪinf<6E>ı<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩƥ<EFBFBD><EFBFBD>򽫱<EFBFBD>Ȩȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
const int MAXN = 210;
int nx, ny; // <20><><EFBFBD>ߵĵ<DFB5><C4B5><EFBFBD><EFBFBD>Ŵ<EFBFBD>1<EFBFBD><31>nx<6E><78><EFBFBD>ұߵ<D2B1><DFB5><EFBFBD><EFBFBD>Ŵ<EFBFBD>1<EFBFBD><31>ny
long long inf, cost[MAXN][MAXN], fx[MAXN], fy[MAXN], dist[MAXN]; //Ȩֵ<C8A8><D6B5>Ϊlong long<6E>Ļ<EFBFBD><C4BB><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
int used[MAXN], maty[MAXN], which[MAXN];
inline void AddEdge(int x, int y, int z)
{
cost[x][y] = z;
}
pair<int, long long> KM(void)
{
for (int x = 1; x <= nx; x++)
{
int y0 = 0;
maty[0] = x;
for (int y = 0; y <= ny; y++)
{
dist[y] = inf + 1;
used[y] = false;
}
do
{
used[y0] = true;
int x0 = maty[y0], y1;
long long delta = inf + 1;
for (int y = 1; y <= ny; y++) if (!used[y])
{
long long curdist = cost[x0][y] - fx[x0] - fy[y];
if (curdist < dist[y])
{
dist[y] = curdist;
which[y] = y0;
}
if (dist[y] < delta)
{
delta = dist[y];
y1 = y;
}
}
for (int y = 0; y <= ny; y++) if (used[y])
{
fx[maty[y]] += delta;
fy[y] -= delta;
}
else dist[y] -= delta;
y0 = y1;
}
while (maty[y0] != 0);
do
{
int y1 = which[y0];
maty[y0] = maty[y1];
y0 = y1;
}
while (y0);
}
long long ret = 0;
int npair = 0;
for (int y = 1; y <= ny; y++)
{
int x = maty[y];
if (cost[x][y] < inf)
{
ret += cost[x][y];
npair++;
}
}
return make_pair(npair, ret);
}
inline void clear(void)
{
memset(fx, 0x9f, sizeof fx);
memset(fy, 0x9f, sizeof fy);
memset(cost, 0x3f, sizeof cost);
memset(maty, 0, sizeof maty);
inf = cost[0][0];
}
==================Dinic<69><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>Ż<EFBFBD>
struct Edge
{
int y, f;
Edge *next, *opt;
}*a[MAXN], DATA[MAXM << 1], *data = DATA;
inline void Add(int x, int y, int c)
{
Edge *tmp = data++;
tmp->y = y;
tmp->f = c, tmp->next = a[x];
a[x] = tmp;
tmp = data++;
tmp->y = x;
tmp->f = 0;
tmp->next = a[y];
a[y] = tmp;
a[x]->opt = a[y];
a[y]->opt = a[x];
}
int n, m, vs, vt, L;
int level[MAXN], d[MAXN];
inline bool Bfs(void)
{
memset(level, -1, sizeof level);
d[1] = vs;
level[vs] = 0;
int head = 1, tail = 1;
while (head <= tail)
{
int now = d[head++];
e[now] = a[now];
for (Edge *p = a[now]; p; p = p->next) if (p->f > 0&& level[p->y] == -1)
level[d[++tail] = p->y] = level[now] + 1;
}
return level[vt] != -1;
}
inline int Extend(int u, int sum)
{
if (u == vt) return sum;
int r = 0, t;
for (Edge *p = e[u]; p && r < sum; p = p->next) if (level[p->y] == level[u] + 1 && p->f > 0)
{
t = std::min(sum - r, p->f);
t = Extend(p->y, t);
p->f -= t, p->opt->f += t, r += t;
e[u] = p;
}
if (!r) level[u] = -1;
return r;
}
inline int Dinic(void)
{
int r = 0, t;
while (Bfs())
{
while ((t = Extend(vs, inf))) r += t;
}
return r;
}
============spfa<66><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Edge
{
int y, f, c;
Edge *next, *opt;
Edge(int y, int f, int c, Edge *next):y(y), f(f), c(c), next(next) {}
}*a[MAXN];
inline void AddEdge(int x, int y, int f, int c)
{
a[x] = new Edge(y, f, c, a[x]);
a[y] = new Edge(x, 0, -c, a[y]);
a[x]->opt = a[y];
a[y]->opt = a[x];
}
int d[MAXN], vis[MAXN], dis[MAXN];
Edge *path[MAXN];
inline pair<int, int> Spfa(void)
{
int Flow = 0, Cost = 0;
while (true)
{
memset(vis, 0, sizeof vis);
memset(dis, 0x7f, sizeof dis);
memset(path, 0, sizeof path);
int head = 1, tail = 1, sum = 1;
d[1] = vs;
vis[vs] = true;
dis[vs] = 0;
while (sum)
{
int now = d[head++];
if (head == MAXN) head = 1;
sum--;
for (Edge *p = a[now]; p; p = p->next) if (p->f > 0 && dis[p->y] > dis[now] + p->c)
{
dis[p->y] = dis[now] + p->c;
path[p->y] = p;
if (!vis[p->y])
{
++tail;
if (tail == MAXN) tail = 1;
sum++;
d[tail] = p->y;
vis[p->y] = true;
}
}
vis[now] = false;
}
if (dis[vt] == dis[0]) return make_pair(Flow, Cost);
int tmp = vt, Min = ~0U>>1;
while (path[tmp])
{
Min = min(Min, path[tmp]->f);
tmp = path[tmp]->opt->y;
}
Flow += Min;
tmp = vt;
while (path[tmp])
{
path[tmp]->f -= Min;
path[tmp]->opt->f += Min;
Cost += Min * path[tmp]->c;
tmp = path[tmp]->opt->y;
}
}
return make_pair(Flow, Cost);
}
================<3D><>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ͼ<EFBFBD>
ʹ<EFBFBD><EFBFBD><EFBFBD>ڽӾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1<><31>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>tmp[i].second<6E><64>ʾi<CABE><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
map[i][j] = 1 <20><>ʾ<EFBFBD><CABE>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>i<EFBFBD><69>j<EFBFBD><6A><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
dis[i][j] <20><>ʾ<EFBFBD><CABE>i<EFBFBD><69>j<EFBFBD>ߵij<DFB5><C4B3><EFBFBD>
typedef pair<int, int> PII;
PII tmp[MAXN];
int dis[MAXN][MAXN];
int map[MAXN][MAXN];
int vis[MAXN], q[MAXN], inp[MAXN];
int Dfs(int u)
{
vis[u] = true;
int ret = 1;
for (int i = 1; i <= n; i++) if (map[u][i] && !vis[i]) ret += Dfs(i);
return ret;
}
int Zhuliu(void)
{
memset(map, 0, sizeof map);
memset(vis, 0, sizeof vis);
if (Dfs(1) != n) return -1;
int done = 0;
while (true)
{
memset(vis, 0, sizeof vis);
memset(inp, 0, sizeof inp);
int Ans = 0;
for (int i = 1; i <= n; i++) vis[i] = i;
for (int i = 2; i <= n; i++)
{
tmp[i] = PII(1000000000, 0);
for (int j = 1; j <= n; j++) if (map[j][i] == 1) tmp[i] = min(tmp[i], PII(dis[j][i], j));
inp[tmp[i].second]++;
if (tmp[i].second) Ans += tmp[i].first;
}
int head = 1, tail = 0;
for (int i = 1; i <= n; i++) if (!inp[i]) q[++tail] = i;
while (head <= tail)
{
int now = q[head++];
if (!--inp[tmp[now].second]) q[++tail] = tmp[now].second;
}
bool ok = true;
for (int i = 1, t; i <= n; i++) if (inp[i] > 0)
{
t = i;
ok = false;
do
{
inp[t] = -i;
t = tmp[t].second;
vis[t] = i;
}
while (t != i);
}
if (ok) return Ans + done;
for (int i = 1; i <= n; i++) if (inp[i] < 0)
{
done += tmp[i].first;
for (int j = 1; j <= n; j++) if (vis[j] != vis[i])
{
if (map[i][j])
{
map[vis[i]][vis[j]] = 1;
dis[vis[i]][vis[j]] = min(dis[vis[i]][vis[j]], dis[i][j]);
}
if (map[j][i])
{
dis[vis[j]][vis[i]] = min(dis[vis[j]][vis[i]], dis[j][i] - tmp[i].first);
map[vis[j]][vis[i]] = 1;
}
}
if (vis[i] != i) for (int j = 1; j <= n; j++) map[i][j] = map[j][i] = 0;
}
}
}