QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#702970 | #5548. Increase the Toll Fees | MeatInTheMiddle# | WA | 2ms | 13844kb | C++20 | 3.4kb | 2024-11-02 16:53:03 | 2024-11-02 16:53:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 202020; // PLZ CHK!
typedef array<ll,4> arr;
struct dsu {
vector<int> p;
int n;
void init(int _n) {
n=_n+10;
p.resize(n);
iota(all(p),0);
}
int fd(int a) {
if (a==p[a]) return a;
return p[a]=fd(p[a]);
}
int mg(int a, int b) {
a=fd(a), b=fd(b);
if (a==b) return 0;
p[b]=a; return 1;
}
} dsu1, dsu2;
int N,M;
arr E[MAX],E2[MAX];
int usd[MAX],par[MAX][20],cst[MAX][20],dep[MAX];
vector<pii> G[MAX];
void dfs(int cur, int prv) {
for (auto [nxt,val]:G[cur]) {
if (nxt==prv) continue;
par[nxt][0]=cur, cst[nxt][0]=val;
dep[nxt]=dep[cur]+1;
dfs(nxt,cur);
}
}
void init() {
for (int j=1; j<20; j++) {
for (int i=1; i<=N; i++) {
par[i][j]=par[par[i][j-1]][j-1];
cst[i][j]=max(cst[i][j-1],cst[par[i][j-1]][j-1]);
}
}
}
int lca(int u, int v) {
int t1=u,t2=v;
int res=0;
if (dep[u]>dep[v]) swap(u,v);
for (int i=19; i>=0; i--) {
if (dep[par[v][i]]>=dep[u]) {
res=max(res, cst[v][i]);
v=par[v][i];
}
}
if (u==v) {
return res;
}
for (int i=19; i>=0; i--) {
if (par[v][i]!=par[u][i]) {
res=max(res, cst[v][i]);
res=max(res, cst[u][i]);
v=par[v][i];
u=par[u][i];
}
}
return max({res, cst[u][0], cst[v][0]});
}
int main() {
fio();
cin>>N>>M;
for (int i=0; i<M; i++) {
int u,v,w; cin>>u>>v>>w;
E[i]={u,v,w,i};
E2[i]=E[i];
}
dsu1.init(N+1),dsu2.init(N+1);
sort(E,E+M,[](auto a, auto b){ return a[2]<b[2]; });
for (int i=0,c=0; i<M && c<N-1; i++) {
auto [u,v,w,idx]=E[i];
if (dsu1.mg(u,v)) {
usd[idx]=1;
c++;
}
}
for (int i=0,c=0; i<M && c<N-1; i++) {
auto [u,v,w,idx]=E[i];
if (usd[idx]) continue;
if (dsu2.mg(u,v)) {
G[u].pb({v,w}), G[v].pb({u,w});
c++;
}
if (i==M-1 && c<N-1) {
cout<<-1;
return 0;
}
}
dfs(1,0);
init();
ll ans=0;
for (int i=0; i<M; i++) {
auto [u,v,w,idx]=E2[i];
if (usd[i]) {
ll t=lca(u,v)+1;
ans+=max(0ll,t-w);
}
}
cout<<ans;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11924kb
input:
4 6 1 2 2 1 3 5 1 4 5 2 3 3 2 4 5 3 4 4
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 2ms
memory: 7788kb
input:
3 4 1 2 3 2 3 4 1 3 5 1 3 10
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 13844kb
input:
5 10 1 2 14 1 3 14 1 4 9 1 5 15 2 3 8 2 3 10 2 4 13 3 4 8 4 5 10 4 5 15
output:
21
result:
ok single line: '21'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 7772kb
input:
2 1 1 2 1
output:
0
result:
wrong answer 1st lines differ - expected: '-1', found: '0'