# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
433788 | Mounir | Bridges (APIO19_bridges) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define chmax(x, v) x = max(x, v)
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define int long long
using namespace std;
const int N = (1 << 18), B = 18;
struct Arete {
int noeud, voisin, poids;
bool isArete;
bool operator < (const Arete &autre) const {
if (poids != autre.poids)
return poids < autre.poids;
if (isArete ! =autre.isArete)
return isArete;
if (noeud != autre.noeud)
return noeud < autre.noeud;
return voisin < autre.voisin;
}
};
int numNoeud[N], numVoisin[N];
int noeudArete[N], voisinArete[N];
bool vu[N];
int ens[N], taille[N];
int getEns(int noeud){
if (ens[noeud] != noeud)
ens[noeud] = getEns(ens[noeud]);
return ens[noeud];
}
vector<pii> voisins[N];
int ancetres[N][B], maxi[N][B];
int prof[N];
int ans[N];
signed main(){
int nNoeuds, nAretes; cin >> nNoeuds >> nAretes;
vector<Arete> aretes;
for (int iArete = 0; iArete < nAretes; ++iArete){
int noeud, voisin, poids; cin >> noeud >> voisin >> poids;
--noeud, --voisin;
aretes.pb({noeud, voisin, poids, true});
}
int nReqs; cin >> nReqs;
for (int iReq = 0; iReq < nReqs; ++iReq){
int type, noeud, poids; cin >> type >> noeud >> poids;
aretes.pb({noeud - 1, iReq, poids, false});
}
for (int noeud = 0; noeud < nNoeuds; ++noeud)
ens[noeud] = noeud, taille[noeud] = 1;
sort(all(aretes));
for (Arete& arete : aretes){
if (arete.isArete){
int a = arete.noeud, b = arete.voisin;
arete.noeud = getEns(arete.noeud);
arete.voisin = getEns(arete.voisin);
if (arete.noeud != arete.voisin){
ens[arete.noeud] = arete.voisin;
taille[arete.voisin] += taille[arete.noeud];
voisins[a].pb({b, arete.poids});
voisins[b].pb({a, arete.poids});
}
}
else
ans[arete.voisin] = taille[getEns(arete.noeud)];
}
for (int iReq = 0; iReq < nReqs; ++iReq){
cout << ans[iReq] << endl;
}
return 0;
}