QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196609#6526. Canvasucup-team870#ML 3ms57868kbC++142.6kb2023-10-01 20:15:452023-10-01 20:15:45

Judging History

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

  • [2023-10-01 20:15:45]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:57868kb
  • [2023-10-01 20:15:45]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

const int N = 1000010;

vector <int> ef[N];
vector <P> eb[N];

int ans[N], tag[N], ss, tt, inq[N], dfn[N], sum, rere[N], yes[N];
set<P> ksj;

void erase(int s) {
    queue <int> q;
    q.push(s);
    inq[s] = 1;
    ksj.erase(P(dfn[s], s));
    sum += 1;
    // cout <<s<<"#\n";
    while (!q.empty()) {
        int u = q.front();
        
        q.pop();
        ksj.erase(P(dfn[u], u));
        for (auto t: eb[u]) {
            int v = t.first;
            ans[t.second] = tt--;
            if (!inq[v]) q.push(v), inq[v] = 1,sum += 2;
        }
    }
}

void dfs(int u) {
    int mx = 0;
    for (auto v: ef[u]) {
        if (dfn[v]) {
            mx = max(mx, dfn[v]);
        } else {
            dfs(v);
            mx = max(mx, dfn[v]);
        }
    }
    dfn[u] = mx + 1;
}

int main() {
    int T; scanf("%d", &T);
    while (T--) {
        int n, m; scanf("%d%d", &n, &m);
        ss = 1, tt = m;
        rep (i, 1, m) {
            int l, r, x, y;
            scanf("%d%d%d%d", &l, &x, &r, &y);
            yes[l] = yes[r] = 1;
            if (x == 1 && y == 1) {
                ans[i] = ss++;
            } 
            else if (x == 2 && y == 2) {
                ans[i] = tt--;
                tag[l] = tag[r] = 2;
            }
            else {
                if (x < y) swap(l, r), swap(x, y);
                ef[l].push_back(r);
                eb[r].push_back(P(l, i));
            }
        }
        // rep (i, 1, n) if (tag[i] == 2) sum += 2;

        rep (i, 1, n) {
            if (yes[i] && !dfn[i]) dfs(i);
        }
        rep (i, 1, n) {
            if (yes[i]) ksj.insert(P(dfn[i], i));
        }

        rep (i, 1, n) {
            if (tag[i] == 2 && !inq[i]) {
                erase(i);
                sum += 1;
            }
        }
        
        
        while (ksj.size()) {
            int u = (*ksj.begin()).second;
            // ksj.erase(ksj.begin());
            erase(u);
        }
        cout << sum <<'\n';
        rep (i, 1, m) rere[ans[i]] = i;
        rep (i, 1, m) {
            cout << rere[i] <<' ';
        }
        cout <<'\n';
        rep (i, 1, n) inq[i] = tag[i] = dfn[i] = 0, ef[i].clear(), eb[i].clear(), yes[i] =0;
        ksj.clear();
        rep (i, 1, m) ans[i] = 0;
        sum = 0;
    }

    return 0;
}

/*

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1
*/

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 57868kb

input:

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1

output:

7
4 2 1 3 
5
2 1 

result:

ok Correct. (2 test cases)

Test #2:

score: -100
Memory Limit Exceeded

input:

1
10 13
1 1 2 2
2 1 3 2
1 2 3 1
3 1 4 2
4 1 5 2
5 1 6 2
4 2 6 1
7 1 8 2
8 1 9 2
7 2 9 1
5 2 9 1
8 2 10 2
1 1 10 1

output:


result: