QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#555842#6526. CanvaschimeraRE 0ms3596kbC++113.4kb2024-09-10 10:53:272024-09-10 10:53:27

Judging History

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

  • [2024-09-10 10:53:27]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3596kb
  • [2024-09-10 10:53:27]
  • 提交

answer

#include <bits/stdc++.h>
#define greedy int
#define mindset main()

using namespace std;
#define FOR(a,b,c) for(ll a = b; a < (c); a++)
#define FORR(a,b,c) for(ll a = b; a > (c); a--)
 
#define READ(x) ll x;cin>>x;
#define READAR(x,n) vll x(n); FOR(readar,0,n) cin >> x[readar];
#define READS(x) string x; cin>>x;
#define SEP(n,mx) (((n) == (mx)-1) ? '\n' : ' ')

#define speedfirst ios_base::sync_with_stdio(false); cin.tie(NULL); 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef tuple<ll,ll,ll> tll;

#define rep FOR
#define vi vll
#define sz(x) ((ll)x.size())


vi val, comp, z, cont, ind;
int Time, ncomps;
template<class G, class F> int dfs(int j, G& g, F& f) {
ll low = val[j] = ++Time, x; z.push_back(j);
for (auto e : g[j]) if (comp[e.first] < 0)
low = min(low, val[e.first] ?: dfs(e.first,g,f));
if (low == val[j]) {
do {
x = z.back(); z.pop_back();
comp[x] = ncomps;
cont.push_back(x);
} while (x != j);
f(cont); cont.clear();
ncomps++;
}
return val[j] = low;
}
template<class G, class F> void scc(G& g, F f) {
int n = sz(g);
val.assign(n, 0); comp.assign(n, -1);
ind.assign(n,0);
Time = ncomps = 0;
rep(i,0,n) if (comp[i] < 0) dfs(i, g, f);
}

void solve() {
    READ(N); READ(M);

    vector<array<ll,4>> eds(M);
    for(ll i = 0; i < M; i++)  { 
        for(ll j = 0; j < 4; j++) cin >> eds[i][j];
        eds[i][0]--; eds[i][2]--;
        if(eds[i][1] == 2) {
            swap(eds[i][0], eds[i][2]);
            swap(eds[i][1], eds[i][3]);
        }
    }

    vector<bool> two(N, false);

    vll out;

    vll twos;

    vector<vpll> adj(N);

    for(ll i = 0; i < M; i++) {
        if(eds[i][1] == 1 && eds[i][3] == 1) {
            out.push_back(i);
        } else if(eds[i][1] == 2 && eds[i][3] == 2) {
            twos.push_back(i);
            two[eds[i][0]] = true;
            two[eds[i][2]] = true;
        } else {
            adj[eds[i][0]].push_back({eds[i][2], i});
        }
    }

    vll mid1;
    vll mid_rev = {};

    scc(adj, [&](const vi& cont) {
        for(auto x: cont) for(auto a: adj[x]) ind[comp[a.first]]++;
    });

    vector<bool> visited(N, false);

    function<void(ll)> destroy = [&](ll x) {
        if(visited[x]) return;
        visited[x] = true;
        vll vq = {x};
        while(vq.size()) {
            ll b = vq.back(); vq.pop_back();
            for(auto a: adj[b]) {
                if(visited[a.first]) mid1.push_back(a.second); // ignore and overwrite.
                else {
                    mid_rev.push_back(a.second); visited[a.first] = true;
                    vq.push_back(a.first);
                }
            }
        }
        return;
    };

    for(ll i = 0; i < N; i++) {
        if(ind[comp[i]] == 0 && two[i]) destroy(i);
    }
    for(ll i = 0; i < N; i++) {
        if(ind[comp[i]] == 0) destroy(i);
    }

    reverse(mid_rev.begin(),mid_rev.end());

    for(auto x: mid1) out.push_back(x); for(auto x: mid_rev) out.push_back(x); for(auto x: twos) out.push_back(x);

    vll fin(N,0);
    for(auto x: out) {
        fin[eds[x][0]]=eds[x][1];
        fin[eds[x][2]]=eds[x][3];
    }

    ll su = 0; for(auto x: fin) su += x;

    assert(out.size() == M);

    cout << su << "\n";
    for(ll i = 0; i < M; i++) cout << out[i]+1 << (i == M-1 ? '\n': ' ');
}

greedy mindset {
    speedfirst;
    READ(T); FOR(t,0,T) solve();
}

詳細信息

Test #1:

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

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
Runtime Error

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: