mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-27 17:23:17 +00:00
213 lines
6.9 KiB
Python
Executable file
213 lines
6.9 KiB
Python
Executable file
"""#
|
||
|
||
### 谜题描述
|
||
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.
|
||
|
||
However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.
|
||
|
||
More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold:
|
||
|
||
1. for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges;
|
||
2. vertex with number 1 has no incoming edges;
|
||
3. the obtained directed graph does not have cycles.
|
||
|
||
Input
|
||
|
||
The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci.
|
||
|
||
It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists.
|
||
|
||
Output
|
||
|
||
Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input.
|
||
|
||
If there are several solutions you can print any of them.
|
||
|
||
Examples
|
||
|
||
Input
|
||
|
||
3 3
|
||
3 2 10
|
||
1 2 10
|
||
3 1 5
|
||
|
||
|
||
Output
|
||
|
||
1
|
||
0
|
||
1
|
||
|
||
|
||
Input
|
||
|
||
4 5
|
||
1 2 10
|
||
1 3 10
|
||
2 3 5
|
||
4 2 15
|
||
3 4 5
|
||
|
||
|
||
Output
|
||
|
||
0
|
||
0
|
||
1
|
||
1
|
||
0
|
||
|
||
Note
|
||
|
||
In the first test case, 10 flow units pass through path <image>, and 5 flow units pass directly from source to sink: <image>.
|
||
|
||
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
|
||
```python
|
||
#include <bits/stdc++.h>
|
||
using namespace std;
|
||
int inflow[200005], outflow[200005], cost[200005], A[200005], B[200005];
|
||
int ans[200005];
|
||
vector<pair<int, int> > adj[200005];
|
||
vector<pair<int, int> >::iterator it;
|
||
int main() {
|
||
memset(ans, -1, sizeof(ans));
|
||
int N, M;
|
||
scanf(\"%d%d\", &N, &M);
|
||
for (int i = 0; i < M; ++i) {
|
||
int a, b;
|
||
scanf(\"%d%d%d\", &a, &b, &cost[i]);
|
||
adj[a].push_back(make_pair(b, i));
|
||
adj[b].push_back(make_pair(a, i));
|
||
outflow[a] += cost[i];
|
||
outflow[b] += cost[i];
|
||
A[i] = a;
|
||
B[i] = b;
|
||
}
|
||
queue<int> Q;
|
||
Q.push(1);
|
||
while (!Q.empty()) {
|
||
int x = Q.front();
|
||
Q.pop();
|
||
for (it = adj[x].begin(); it != adj[x].end(); ++it) {
|
||
int y = it->first, id = it->second;
|
||
if (ans[id] != -1) continue;
|
||
ans[id] = (A[id] == x) ? 0 : 1;
|
||
inflow[y] += cost[id];
|
||
outflow[y] -= cost[id];
|
||
if (inflow[y] == outflow[y] && y != N) Q.push(y);
|
||
}
|
||
}
|
||
for (int i = 0; i < M; ++i) printf(\"%d\n\", ans[i]);
|
||
}
|
||
```
|
||
|
||
|
||
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
|
||
"""
|
||
|
||
from bootcamp import Basebootcamp
|
||
import random
|
||
import re
|
||
from collections import defaultdict, deque
|
||
from bootcamp import Basebootcamp
|
||
|
||
class Eflawedflowbootcamp(Basebootcamp):
|
||
def __init__(self, max_n=5, max_m=10):
|
||
self.max_n = max_n
|
||
self.max_m = max_m
|
||
|
||
def case_generator(self):
|
||
# 逆向构造法:先生成解再构建输入
|
||
n = random.randint(2, self.max_n)
|
||
m = random.randint(n-1, min(self.max_m, n*(n-1)//2))
|
||
|
||
# 生成树结构保证连通性
|
||
nodes = list(range(1, n+1))
|
||
random.shuffle(nodes)
|
||
edges = []
|
||
parent = {nodes[0]}
|
||
for node in nodes[1:]:
|
||
u = random.choice(list(parent))
|
||
edges.append((u, node, random.randint(1, 10000)))
|
||
parent.add(node)
|
||
|
||
# 添加额外边
|
||
possible = [(i,j) for i in range(1,n+1) for j in range(i+1,n+1)
|
||
if not any((i==u and j==v) or (i==v and j==u) for u,v,_ in edges)]
|
||
for _ in range(m - len(edges)):
|
||
u, v = random.choice(possible)
|
||
possible.remove((u,v))
|
||
edges.append((u, v, random.randint(1, 10000)))
|
||
|
||
# 生成合法方向
|
||
adj = defaultdict(list)
|
||
for idx, (u, v, c) in enumerate(edges):
|
||
adj[u].append((v, idx))
|
||
adj[v].append((u, idx))
|
||
|
||
ans = [-1]*len(edges)
|
||
inflow = defaultdict(int)
|
||
outflow = defaultdict(int)
|
||
for u, v, c in edges:
|
||
outflow[u] += c
|
||
outflow[v] += c
|
||
|
||
q = deque([1])
|
||
while q:
|
||
x = q.popleft()
|
||
for y, idx in adj[x]:
|
||
if ans[idx] != -1: continue
|
||
u, v, c = edges[idx]
|
||
ans[idx] = 0 if x == u else 1
|
||
inflow[y] += c
|
||
outflow[y] -= c
|
||
if y != n and inflow[y] == outflow[y]:
|
||
q.append(y)
|
||
|
||
# 打乱边顺序
|
||
edges_with_ids = list(enumerate(edges))
|
||
random.shuffle(edges_with_ids)
|
||
solution = [0]*len(edges)
|
||
case_edges = []
|
||
for new_idx, (orig_idx, (u, v, c)) in enumerate(edges_with_ids):
|
||
case_edges.append({'a':u, 'b':v, 'c':c})
|
||
solution[new_idx] = ans[orig_idx]
|
||
|
||
return {
|
||
'n': n,
|
||
'm': m,
|
||
'edges': case_edges,
|
||
'solution': solution
|
||
}
|
||
|
||
@staticmethod
|
||
def prompt_func(question_case) -> str:
|
||
input_lines = [f"{question_case['n']} {question_case['m']}"]
|
||
input_lines += [f"{e['a']} {e['b']} {e['c']}" for e in question_case['edges']]
|
||
input_str = '\n'.join(input_lines) # 提前生成完整输入字符串
|
||
return f"""你正在解决一个无向图最大流方向确定问题。给定包含{question_case['n']}个顶点和{question_case['m']}条边的无向图,请确定每条边的方向,满足:
|
||
1. 中间顶点(非源点1和汇点n)的入流等于出流
|
||
2. 源点没有入边
|
||
3. 图中无环
|
||
|
||
输入格式:
|
||
{input_str}
|
||
|
||
请输出m行0/1(输入顺序对应每条边的方向),答案用[answer]和[/answer]包裹。"""
|
||
|
||
@staticmethod
|
||
def extract_output(output):
|
||
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
|
||
if not matches:
|
||
return None
|
||
solution = []
|
||
for line in matches[-1].strip().split('\n'):
|
||
line = line.strip()
|
||
if line in ('0', '1'):
|
||
solution.append(int(line))
|
||
return solution if len(solution) == len(matches[-1].strip().split('\n')) else None
|
||
|
||
@classmethod
|
||
def _verify_correction(cls, solution, identity):
|
||
return solution == identity['solution']
|