QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#596525#2543. Edges, Colors and MSTgambit#TL 0ms3548kbC++172.9kb2024-09-28 15:57:462024-09-28 15:57:46

Judging History

你现在查看的是最新测评结果

  • [2024-09-28 15:57:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3548kb
  • [2024-09-28 15:57:46]
  • 提交

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] << ' ';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3548kb

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

output:


result: