이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=505;
int N,M,Q;
struct DSU {
int par[MX],sz[MX];
int fd(int v) {
return par[v]==v?v:par[v]=fd(par[v]);
}
bool merge(int u, int v) {
u=fd(u),v=fd(v);
if(u==v) return false;
if(sz[u]>sz[v]) swap(u,v);
par[u]=v;
sz[v]+=sz[u];
return true;
}
void reset() {
for(int i=1;i<=N;i++) par[i]=i,sz[i]=1;
}
} ds;
set<array<int,3>> adj[MX];
const int inf=2e9;
pair<int,int> dist[MX];
pair<int,int> bfs(int S, int T) {
queue<int> q;
q.push(S);
for(int i=1;i<=N;i++) dist[i]={-1,-1};
dist[S]={inf,inf};
while(!q.empty()) {
auto v=q.front(); q.pop();
if(v==T) break;
for(auto [u,w,ii]:adj[v]) {
if(dist[u].first!=-1) continue;
dist[u]=min(make_pair(w,ii),dist[v]);
q.push(u);
}
}
return dist[T];
}
pair<int,int> BFS(int S, int T) {
queue<int> q;
q.push(S);
for(int i=1;i<=N;i++) dist[i]={-1,-1};
dist[S]={0,0};
while(!q.empty()) {
auto v=q.front(); q.pop();
if(v==T) break;
for(auto [u,w,ii]:adj[v]) {
if(dist[u].first!=-1) continue;
dist[u]=max(make_pair(w,ii),dist[v]);
q.push(u);
}
}
return dist[T];
}
array<int,3> e[100000+5];
vector<int> PF[100000+5], SF[100000+5];
set<array<int,4>> E;
map<array<int,3>, int> id;
int L[100000+5], R[100000+5];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>M;
vector<array<int,4>> edges;
for(int i=0;i<M;i++) {
int u,v,w;
cin>>u>>v>>w;
e[i]={w,u,v};
id[e[i]]=i;
edges.push_back({w,u,v,i});
}
sort(edges.begin(),edges.end());
int i=0;
for(auto [w,u,v,ID]:edges) {
auto [ww,ii]=bfs(u,v);
auto [WW,uu,vv]=e[ii];
if(ww!=-1) {
adj[uu].erase({vv,ww,ii});
adj[vv].erase({uu,ww,ii});
E.erase({ww,uu,vv,ii});
}
adj[u].insert({v,w,ID});
adj[v].insert({u,w,ID});
E.insert({w,u,v,ID});
for(auto [ww,uu,vv,id]:E) {
PF[i].push_back(id);
}
reverse(PF[i].begin(),PF[i].end());
if(ww!=-1)
L[ID]=(w+ww),assert(ww<=w);
else
L[ID]=0;
i++;
}
reverse(edges.begin(),edges.end());
E.clear();
for(int i=1;i<=N;i++) adj[i].clear();
i=M-1;
for(auto [w,u,v,ID]:edges) {
auto [ww,ii]=BFS(u,v);
auto [WW,uu,vv]=e[ii];
if(ww!=-1) {
adj[uu].erase({vv,ww,ii});
adj[vv].erase({uu,ww,ii});
E.erase({ww,uu,vv,ii});
}
adj[u].insert({v,w,ID});
adj[v].insert({u,w,ID});
E.insert({w,u,v,ID});
for(auto [ww,uu,vv,id]:E) {
SF[i].push_back(id);
}
if(ww!=-1)
R[ID]=(w+ww),assert(ww>=w);
else
R[ID]=2e9+1;
i--;
}
reverse(edges.begin(),edges.end());
vector<pair<int,int>> add, del;
for(int i=0;i<M;i++) {
add.push_back({L[i],i});
del.push_back({R[i],i});
}
sort(add.begin(),add.end());
sort(del.begin(),del.end());
int pa=-1,pd=-1;
set<int> curEdges;
cin>>Q;
for(int i=0;i<Q;i++) {
ll x;
cin>>x;
while(pa+1<add.size() && add[pa+1].first<=2*x) {
curEdges.insert(add[pa+1].second);
pa++;
}
while(pd+1<del.size() && del[pd+1].first<=2*x) {
curEdges.erase(del[pd+1].second);
pd++;
}
ll res=0;
for(auto y:curEdges) res+=abs(e[y][0]-x);
cout<<res<<'\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:174:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
174 | while(pa+1<add.size() && add[pa+1].first<=2*x) {
| ~~~~^~~~~~~~~~~
reconstruction.cpp:179:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
179 | while(pd+1<del.size() && del[pd+1].first<=2*x) {
| ~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |