QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#596559 | #2543. Edges, Colors and MST | gambit# | TL | 0ms | 3564kb | C++17 | 2.9kb | 2024-09-28 16:00:25 | 2024-09-28 16:00:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct Node {
int next, color, idx;
bool operator<(const Node a) const {
if(this->color == a.color) return this->idx < a.idx;
return this->color > a.color;
}
};
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int N, M; cin >> N >> M;
vector<vector<Node>> conn(N);
vector<tuple<int, int, int>> v;
for(int i=0;i<M;i++) {
int a, b, c; cin >> a >> b >> c;
if(a>b) swap(a, b);
v.push_back({a-1, b-1, c}); // {a, b, color}
conn[a-1].push_back({b-1, c, i});
conn[b-1].push_back({a-1, c, i});
}
for(int i=0;i<N;i++) sort(conn[i].begin(), conn[i].end());
int cur=1;
int res[M] = {0, };
for(int i=0;i<M;i++) {
if(!res[i]) {
if(get<2>(v[i])==0) {
vector<int> next;
for(Node nn:conn[get<0>(v[i])]) {
if(nn.color==0) break;
next.push_back(nn.idx);
}
sort(next.begin(), next.end());
set<int> endSet;
for(Node nn:conn[get<1>(v[i])]) {
if(nn.color==0) break;
endSet.insert(nn.idx);
}
bool finish=false;
int curVisited[M] = {0, };
set<int> idxs;
for(int start:next) {
if(finish) break;
curVisited[start]=-1;
queue<int> q; q.push(start);
while(!q.empty()) {
int ccc = q.front(); q.pop();
if(endSet.count(ccc)) {
for(int ii=ccc;ii!=-1;ii=curVisited[ii]) {
idxs.insert(ii);
}
finish=true;
break;
}
for(Node nnn:conn[get<0>(v[ccc])]) {
if(nnn.color==0) break;
if(!curVisited[nnn.idx]) {
q.push(nnn.idx);
curVisited[nnn.idx]=ccc;
}
}
for(Node nnn:conn[get<1>(v[ccc])]) {
if(nnn.color==0) break;
if(!curVisited[nnn.idx]) {
q.push(nnn.idx);
curVisited[nnn.idx]=ccc;
}
}
}
}
for(int idx:idxs) {
if(!res[idx]) res[idx]=cur++;
}
}
res[i]=cur++;
}
}
for(int i=0;i<M;i++) cout << res[i] << ' ';
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
4 5 1 2 0 2 3 1 3 4 1 2 4 0 1 3 1
output:
3 1 4 5 2
result:
ok 5 number(s): "3 1 4 5 2"
Test #2:
score: -100
Time Limit Exceeded
input:
9 15 1 4 1 3 5 1 3 9 0 1 3 0 2 5 0 5 8 0 6 9 0 8 9 0 1 7 1 1 8 1 6 8 1 4 9 1 2 4 1 3 4 1 4 6 0