QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402194 | #1072. Winter Roads | stasio6# | 100 ✓ | 129ms | 19312kb | C++14 | 3.1kb | 2024-04-30 06:38:44 | 2024-04-30 06:38:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
typedef pair<int, int> pii;
typedef vector<int> vi;
template<class X, class Y> X cmx(X &a, Y b) { a = max<X>(a, b); }
#define ary(k) array<int, k>
int x[100005],y[100005],w[100005],id[100005];
vector <int> use;
int fa[1005],f[100005];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
struct query{
int a,b,lim,id;
};
vector <query> v;
vector <pair<int,int>> cg;
int type[100005],ans[100005];
signed main() {
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
int n,m;
while(1) {
cin >> n >> m;
if(n==0&&m==0) exit(0);
for(int i=1;i<=n;i++) fa[i]=i;
for (int i = 1; i <= m; i++) cin >> x[i] >> y[i] >> w[i], id[i] = i,f[i]=0;
sort(id + 1, id + m + 1, [&](int a, int b) {
return w[a] > w[b];
});
v.clear(),cg.clear(),use.clear();
cg.push_back({1,w[1]});
f[1]=1;
int q;
cin>>q;
for(int i=1;i<=q;i++){
char c='?';
while(c!='B'&&c!='R'&&c!='S') cin>>c;
if(c=='B'||c=='R'){
int xx,yy;
cin>>xx>>yy;
cg.push_back({xx,yy});
f[xx]=1;
type[i]=0;
}
else{
int a,b,ww;
cin>>a>>b>>ww;
v.push_back(query{a,b,ww,i});
type[i]=1;
}
ans[i]=-1;
}
for(int i=1;i<=m;i++){
if(f[id[i]]){
use.push_back(id[i]);
continue;
}
int xx=find(x[id[i]]),yy=find(y[id[i]]);
if(xx!=yy) fa[yy]=xx,use.push_back(id[i]);
}
int now=0,pq=0,pc=0;
while(now<=q){
assert(type[now]==0);
assert(pc<cg.size());
w[cg[pc].first]=cg[pc].second;
pc++,now++;
sort(use.begin(),use.end(),[&](int a,int b){
return w[a]>w[b];
});
vector <query> qrs;
while(now<=q&&type[now]==1){
assert(pq<v.size());
qrs.push_back(v[pq++]);
now++;
}
sort(qrs.begin(),qrs.end(),[&](query a,query b){
return a.lim>b.lim;
});
for(int j=1;j<=n;j++) fa[j]=j;
int usep=0;
for(auto t:qrs){
while(usep<use.size()&&w[use[usep]]>=t.lim){
int xx=find(x[use[usep]]),yy=find(y[use[usep]]);
if(xx!=yy) fa[yy]=xx;
usep++;
}
int xx=find(t.a),yy=find(t.b);
if(xx==yy) ans[t.id]=1;
else ans[t.id]=0;
}
}
for(int i=1;i<=q;i++) if(ans[i]!=-1) cout<<ans[i]<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 129ms
memory: 19312kb
input:
10 14 1 2 3 2 3 5 3 4 7 4 5 1000 5 6 64 6 7 5 7 8 18 8 1 1 1 9 70 2 9 50 8 9 13 9 10 34 7 10 29 5 10 75 25 S 1 1 200 S 1 6 34 S 1 6 35 R 12 35 S 1 6 35 S 1 6 36 B 12 1 S 1 6 1 S 1 6 13 S 1 6 14 R 11 18 S 1 6 18 S 1 6 19 R 7 19 S 1 6 19 R 11 19 S 1 6 19 B 11 18 S 1 6 19 B 7 18 S 1 6 19 R 11 19 S 1 6...
output:
1 1 0 1 0 1 1 0 1 0 0 1 0 0 0 1 1 0 0 0 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 99155 lines