QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#33730 | #2543. Edges, Colors and MST | djs100201 | WA | 9ms | 44356kb | C++ | 3.1kb | 2022-06-04 20:16:01 | 2022-06-04 20:16:04 |
Judging History
answer
#include<bits/stdc++.h>
//#define double long double
//T.erase(unique(T.begin(),T.end()),T.end());
//written by djs100201
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 2e5 + 100, inf = (ll)1e9 * (ll)1e9 + 7, mod = 1e9 + 7, sqrtN = 320;
ll dy[4] = { -1,0,1,0 }, dx[4] = { 0,1,0,-1 };
ll n, m, k, tc = 1, a, b, c, d, sum, x, y, z, base, ans, q;
ll gcd(ll x, ll y) {
if (!y)return x;
return gcd(y, x % y);
}
map<int, int>mp[n_];
vector<ll>v[n_], in[n_], out[n_];
ll res[n_], A[n_], dep[n_], idx[n_], cnt[n_], parent[n_][21];
priority_queue<ll, vector<ll>, greater<>>pq[n_];
multiset<int>st[n_];
void dfs(ll x, ll par) {
dep[x] = dep[par] + 1;
for (auto nxt : v[x]) {
if (nxt == par)continue;
parent[nxt][0] = x;
dfs(nxt, x);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y])swap(x, y);//항상 y의 depth가 더 깊게 만들어주자!
for (int i = 20; i >= 0; i--)
if (dep[y] - dep[x] >= (1ll << i))
y = parent[y][i];
if (x == y)return x;
for (int i = 20; i >= 0; i--) {
if (parent[x][i] != parent[y][i]) {
x = parent[x][i];
y = parent[y][i];
}
}
return parent[x][0];
}
void dfs2(ll x, ll par) {
vector<ll>I, O;
int now = -1, sz = 0;
for (auto nxt : v[x]) {
if (nxt == par)continue;
dfs2(nxt, x);
for (auto i : in[nxt])st[idx[nxt]].insert(i);
for (auto i : out[nxt])st[idx[nxt]].erase(i);
int num = mp[x][nxt];
ll val;
if (st[idx[nxt]].size())val = *st[idx[nxt]].begin();
else val = -1;
A[num] = val;
if (val != -1)pq[val].push(num);
if (sz < st[idx[nxt]].size()) {
sz = st[idx[nxt]].size();
now = nxt;
}
}
if (now == -1)return;
idx[x] = idx[now];
for (auto nxt : v[x]) {
if (nxt == par || nxt == now)continue;
for (auto i = st[idx[nxt]].begin(); i != st[idx[nxt]].end(); i++)st[idx[x]].insert(*i);
st[idx[nxt]].clear();
}
}
void solve() {
cin >> n >> m;
vector<P>edge, all;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
mp[a][b] = i;
mp[b][a] = i;
if (c == 1) {
v[a].push_back(b);
v[b].push_back(a);
}
else edge.push_back({ a,b });
all.push_back({ a,b });
}
dfs(1, 0);
for (int i = 1; i < 21; i++)
for (int j = 1; j <= n; j++)parent[j][i] = parent[parent[j][i - 1]][i - 1];
for (int i = (int)edge.size() - 1; i >= 0; i--) {
a = edge[i].first, b = edge[i].second;
x = lca(a, b);
in[a].push_back(i + 1);
in[b].push_back(i + 1);
out[x].push_back(i + 1);
out[x].push_back(i + 1);
}
for (int i = 1; i <= n; i++)idx[i] = i;
dfs2(1, 0);
base = 1;
ll now = 1;
for (auto nxt : all) {
a = nxt.first, b = nxt.second;
int num = mp[a][b];
if (A[num] == 0) {
while (pq[now].size()) {
a = pq[now].top();
pq[now].pop();
res[a] = base++;
}
now++;
res[num] = base++;
}
else {
if (A[num] == -1)res[num] = base++;
}
}
for (int i = 0; i < m; i++)cout << res[i] << ' ';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
//cin >> tc;
while (tc--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 44356kb
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
Wrong Answer
time: 2ms
memory: 43540kb
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:
4 6 3 5 8 10 12 13 14 9 11 1 7 2 15
result:
wrong answer 1st numbers differ - expected: '1', found: '4'