# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
979549 | vjudge1 | 자매 도시 (APIO20_swap) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
using namespace std;
int tt = 1, n, m;
vector<pair<int, int>> g[200005];
pair<int, int> p[200005];
map<pair<int, int>, int> mp, pos;
set<pair<int, int>> st;
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++){
int a, b, c;
cin >> a >> b >> c;
a++;
b++;
g[a].push_back({b, c});
g[b].push_back({a, c});
mp[{a, b}] = c;
mp[{b, a}] = c;
pos[{a, b}] = i;
pos[{b, a}] = i;
st.insert({c, i});
}
cin >> tt;
while(tt--){
int x, y;
cin >> x >> y;
x++;
y++;
if(n <= 3){
cout << -1 << "\n";
continue;
}
int mx = 0;
if(x != 1) mx = mp[{1, x}];
if(y != 1) mx = max(mx, mp[{1, y}]);
if(x != 1) st.erase({mp[{1, x}], pos[{1, x}]});
if(y != 1) st.erase({mp[{1, y}], pos[{1, y}]});
pair<int, int> p = *st.begin();
st.erase(st.begin());
if(x != 1 && y != 1)
cout << max(mx, p.first) << "\n";
else{
pair<int, int> p1 = *st.begin();
cout << max({mx, p.first, p1.first}) << "\n";
st.insert(p1);
}
st.insert(p);
if(x != 1) st.insert({mp[{1, x}], pos[{1, x}]});
if(y != 1) st.insert({mp[{1, y}], pos[{1, y}]});
}
}