이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n,m,N,q,lg=17,timer=1,ans[100005];
map<pair<int,int>,int> mp;
vector<int> g[100005],d,used,p,Size,g1[100005],dp[20],tin,tout;
multiset<pair<int,int> > s;
vector<pii> Q,ed;
void MS(){
for(int k=1;k<=n;k++){
p[k]=k;
Size[k]=d[k];
}
}
int FS(int v){
if(p[v]==v) return v;
return p[v]=FS(p[v]);
}
void US(int a,int b){
a=FS(a);
b=FS(b);
if(a!=b){
g1[a].push_back(b);
g1[b].push_back(a);
p[b]=a;
Size[a]=min(Size[a],Size[b]);
}
}
void DFS(int v,int p){
used[v]=1;
dp[v][0]=p;
for(int k=1;k<dp[v].size();k++)
dp[v][k]=dp[dp[v][k-1]][k-1];
tin[v]=timer++;
for(int k=0;k<g1[v].size();k++){
int to=g1[v][k];
if(used[to]==0)
DFS(to,v);
}
tout[v]=timer++;
}
bool tree(int a,int b){
if(tin[a]<=tin[b] && tout[a]>=tout[b])
return true;
return false;
}
int LCA(int a,int b){
if(tree(a,b)) return a;
if(tree(b,a)) return b;
int t=a;
for(int k=N;k>=0;k--){
if(dp[t][k]!=0){
int to=dp[t][k];
if(!tree(to,b))
t=to;
}
}
return dp[t][0];
}
main(){
cin>>n>>m;
for(int k=1;k<=m;k++){
int a,b,c;
cin>>a>>b>>c;
mp[{a,b}]=c;
mp[{b,a}]=c;
g[a].push_back(b);
g[b].push_back(a);
}
cin>>N;
d.resize(n+1,INT_MAX);
used.resize(n+1,0);
for(int k=1;k<=N;k++){
int qal;
cin>>qal;
d[qal]=0;
s.insert({0,qal});
}
while(!s.empty()){
int v=s.begin()->second;
used[v]=1;
s.erase(s.begin());
for(int k=0;k<g[v].size();k++){
int to=g[v][k];
if(used[to]==0 && d[to]>d[v]+mp[{v,to}]){
d[to]=d[v]+mp[{to,v}];
s.insert({d[to],to});
}
}
}
for(int k=1;k<=n;k++){
ed.push_back({d[k],k});
}
sort(ed.begin(),ed.end());
reverse(ed.begin(),ed.end());
cin>>q;
for(int k=1;k<=q;k++){
int a,b;
cin>>a>>b;
Q.push_back({a,b});
}
Size.resize(n+1,0);
p.resize(n+1,0);
used.resize(0);
used.resize(n+1,0);
MS();
for(int k=0;k<ed.size();k++){
int v=ed[k].y;
used[v]=1;
for(int k=0;k<g[v].size();k++)
if(used[g[v][k]])
US(v,g[v][k]);
}
used.resize(0);
used.resize(n+1,0);
tin.resize(n+1,0);
tout.resize(n+1,0);
for(int k=0;k<=20;k++)
dp[k].resize(n+1,0);
DFS(ed[ed.size()-1].y,ed[ed.size()-1].y);
for(int k=0;k<Q.size();k++){
int a=Q[k].x,b=Q[k].y;
int lc=LCA(a,b);
cout<<d[lc]<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void DFS(int, int)':
plan.cpp:34:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=1;k<dp[v].size();k++)
~^~~~~~~~~~~~~
plan.cpp:34:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(int k=1;k<dp[v].size();k++)
^~~
plan.cpp:36:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
tin[v]=timer++;
^~~
plan.cpp:37:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<g1[v].size();k++){
~^~~~~~~~~~~~~
plan.cpp: At global scope:
plan.cpp:62:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
plan.cpp: In function 'int main()':
plan.cpp:85:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<g[v].size();k++){
~^~~~~~~~~~~~
plan.cpp:109:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<ed.size();k++){
~^~~~~~~~~~
plan.cpp:112:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<g[v].size();k++)
~^~~~~~~~~~~~
plan.cpp:123:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<Q.size();k++){
~^~~~~~~~~
# | 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... |