محدودیت زمان: ۲ ثانیه
محدودیت حافظه: ۲۵۶ مگابایت
منبع: آزمون عملی دوره ۲۴ المپیاد کامپیوتر
آقای مهندس میخواهد یک گراف «مهندسیساز» بسازد. به یک گراف وزندار مهندسیساز میگوییم اگر بتوان طوری وزن تعدادی از یالهایش را مقدار مثبتی زیاد کرد که هیچکدام از MSFهای گراف سابق در گراف جدید MSF نباشند. آقای مهندس در حال ساختن گرافش است و در هر مرحله یک یال جدید به آن اضافه میکند. شما باید در هر مرحله به او بگویید گرافش مهندسیساز است یا نه.
به یک زیرگراف از گرافمان، جنگل فراگیر میگوییم اگر دور نداشته باشد و بین هر دو راسی که در درون یک مؤلفه قرار دارند، با یالهای این زیرگراف مسیر وجود داشته باشد. به زیرجنگلی که جمع وزن یالهایش کمینه باشد، Minimum Spanning Forest یا MSF میگوییم.
ورودی
در خط اول ورودی دو عدد
n
n و
m
m آمدهاند که
n
n تعداد راسهای گراف و
m
m تعداد یالهای آن را نشان میدهد. در
m
m خط بعد در هر خط سه عدد
u
i
u
i
و
v
i
v
i
و
w
i
w
i
آمدهاست که اضافه شدن یالی به وزن
w
i
w
i
بین دو راس
u
i
u
i
و
v
i
v
i
را نشان میدهد. دقت کنید که یالها ممکن است چندگانه باشند.
1
≤
n
,
m
≤
100
000
1≤n,m≤100 000
1
≤
u
i
,
v
i
≤
n
1≤u
i
,v
i
≤n
1
≤
w
i
≤
1
0
9
1≤w
i
≤10
9
خروجی
در تنها خط خروجی یک رشته به طول
m
m از 0 و 1 چاپ کنید که 1 بودن حرف
i
iام آن به معنی مهندسیساز بودن گراف بعد از اضافه کردن یال
i
iام است.
زیرمسئلهها
زیرمسئله نمره محدودیت
۱ ۱۰
n
,
m
≤
2
000
n,m≤2 000 , وزن تمامی یال ها متفاوت است
۲ ۳۵
n
,
m
≤
2
000
n,m≤2 000
۳ ۵۵ بدون محدودیت اضافی
مثال
ورودی نمونه
6 7
1 2 2
1 3 2
3 2 2
5 6 1
3 5 1
1 4 1
1 5 1
Plain text
خروجی نمونه
0000001 c++
Assistant
To solve the problem, we need to determine at each step whether the graph is "engineer-able" or not. According to the problem, a graph is "engineer-able" if we can increase the weights of some edges so that none of the previous MSFs (Minimum Spanning Forests) remain MSFs after these changes.
An important observation is that the graph becomes engineer-able when the MSF is not unique. If the MSF is unique, even after increasing the weights of some edges, the same MSF remains the only MSF because there are no alternative edges to form a different MSF. However, when the MSF is not unique, we have multiple MSFs, and by increasing the weight of certain edges, we can eliminate some MSFs, making the previous ones no longer MSFs.
To determine whether the MSF is unique at each step, we can use Kruskal's algorithm along with Union-Find (Disjoint Set Union) data structure. We process the edges in order of increasing weight and keep track of whether adding an edge forms a cycle.
When processing edges with the same weight, if we encounter an edge that connects two nodes already in the same component, it means there's a cycle involving edges of equal weight, indicating the MSF is not unique.
Below is the C++ code that implements the above approach:
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
struct Edge {
int u, v, w, idx;
};
int n, m;
vector<Edge> edges;
int parent[MAXN], rank_[MAXN];
// Function to find the parent of a node
int Find(int u) {
if (parent[u] != u)
parent[u] = Find(parent[u]);
return parent[u];
}
// Function to unite two components
bool Union(int u, int v) {
u = Find(u);
v = Find(v);
if (u == v) return false;
if (rank_[u] < rank_[v])
parent[u] = v;
else {
parent[v] = u;
if (rank_[u] == rank_[v])
rank_[u]++;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
edges.resize(m);
for (int i = 0; i < m; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].w;
edges[i].idx = i;
}
// Sort edges by weight and index
sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b) {
if (a.w != b.w)
return a.w < b.w;
return a.idx < b.idx;
});
// Initialize Union-Find structure
for (int i = 1; i <= n; ++i) {
parent[i] = i;
rank_[i] = 0;
}
string ans(m, '0');
bool uniqueMST = true;
int pos = m;
int i = 0;
while (i < m) {
int w = edges[i].w;
vector<Edge> same_weight_edges;
while (i < m && edges[i].w == w) {
same_weight_edges.push_back(edges[i]);
++i;
}
// Temporary parents for detecting cycles among edges of same weight
vector<int> tmp_parent(n + 1);
for (int j = 1; j <= n; ++j)
tmp_parent[j] = Find(j);
// Union-Find for edges of the same weight to detect cycles
for (const Edge &e : same_weight_edges) {
int u = tmp_parent[e.u];
int v = tmp_parent[e.v];
if (u == v) {
// Cycle detected among edges of same minimal weight
// MSF is not unique from this point
uniqueMST = false;
pos = min(pos, e.idx);
break; // No need to check further
} else {
tmp_parent[v] = u;
}
}
// Now, add edges to the main Union-Find
for (const Edge &e : same_weight_edges) {
Union(e.u, e.v);
}
}
// Output the answer
for (int i = 0; i < m; ++i) {
if (i >= pos)
ans[i] = '1';
}
cout << ans << '\n';
return 0;
}
Explanation:
We read all the edges and store them with their indices.
We sort the edges firstly by weight, then by index.
We use the Union-Find data structure to detect cycles and manage disjoint sets.
For each group of edges with the same weight, we create a temporary Union-Find to detect cycles among edges of the same weight without affecting the main Union-Find. If a cycle is found among these edges, the earliest index at which this happens is recorded.
After processing all edges, we determine at which index the graph becomes engineer-able (i.e., when the MST is no longer unique). From that index onward, we set the corresponding positions in the output string to '1'.
This approach ensures that we efficiently detect the point in time when the graph becomes engineer-able, allowing us to produce the correct output within the given time constraints.