Submission #44573326


Source Code Expand

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define ff(i,s,f) for(int i=s;i<f;i++)
#define fb(i,s,f) for(int i=(s)-1;i>=f;i--)
#define ffi(i,s,f) for(int i=s;i<=f;i++)
#define fbi(i,s,f) for(int i=s;i>=f;i--)
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;

const int maxn=2e5+10;

int dsu[maxn];
int siz[maxn];
int cale(int a){
    return dsu[a]==a ? a:dsu[a]=cale(dsu[a]);
}
void join(int a,int b){
    a=cale(a); b=cale(b);
    if(a==b) return;
    if(siz[a]<siz[b]) swap(a,b);
    dsu[b]=a;
    siz[a]+=siz[b];
}

tuple<int,int,char,int> edge[maxn];
int klk=0;
int res[maxn];
bool uradio[maxn];
vector<tuple<int,char,int> > g[maxn];

void solve(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++) dsu[i]=i,siz[i]=1;
    for(int i=1;i<=m;i++){
        int u,v; char c;
        cin>>u>>v>>c;
        edge[i]={u,v,c,i};
        g[u].push_back({v,c,i});
        g[v].push_back({u,c,i});
    }
    string s; cin>>s;
    s='#'+s;

    queue<int> bfs;

    for(int i=1;i<=m;i++){
        auto [u,v,c,e]=edge[i];
        if(s[u]!=s[v]) continue;
        if(s[u]!=c) continue;
        if(cale(u)==cale(v)) continue;
        if(!uradio[u]) bfs.push(u);
        if(!uradio[v]) bfs.push(v);
        uradio[u]=true; uradio[v]=true;
        join(u,v);
        res[klk++]=e;
    }

    while(!bfs.empty()){
        int u=bfs.front();
        bfs.pop();
        for(auto [v,c,e]:g[u]){
            if(uradio[v]) continue;
            if(s[v]!=c) continue;
            res[klk++]=e;
            join(u,v);
            bfs.push(v);
            uradio[v]=true;
        }
    }

    bool da=true;
    for(int i=1;i<=n;i++){
        if(!uradio[i]) da=false;
    }
    for(int i=1;i<=m;i++){
        auto [u,v,c,e]=edge[i];
        if(cale(u)==cale(v)) continue;
        join(u,v);
        res[klk++]=e;
    }
    if(da){
        cout<<"Yes\n";
        for(int i=0;i<klk;i++) cout<<res[i]<<" ";
    }else cout<<"No\n";

}

int main(){
    ios;
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}

Submission Info

Submission Time
Task B - Red and Blue Spanning Tree
User FEDIKUS
Language C++ 20 (gcc 12.2)
Score 600
Code Size 2612 Byte
Status AC
Exec Time 76 ms
Memory 23104 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 77
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 01_small_15.txt, 01_small_16.txt, 01_small_17.txt, 02_dense_00.txt, 02_dense_01.txt, 02_dense_02.txt, 02_dense_03.txt, 02_dense_04.txt, 02_dense_05.txt, 02_dense_06.txt, 02_dense_07.txt, 02_dense_08.txt, 02_dense_09.txt, 02_dense_10.txt, 02_dense_11.txt, 03_rnd_00.txt, 03_rnd_01.txt, 03_rnd_02.txt, 03_rnd_03.txt, 03_rnd_04.txt, 03_rnd_05.txt, 03_rnd_06.txt, 03_rnd_07.txt, 04_same_00.txt, 04_same_01.txt, 04_same_02.txt, 04_same_03.txt, 04_same_04.txt, 04_same_05.txt, 04_same_06.txt, 04_same_07.txt, 05_star_00.txt, 05_star_01.txt, 05_star_02.txt, 05_star_03.txt, 05_star_04.txt, 06_cycle_00.txt, 06_cycle_01.txt, 06_cycle_02.txt, 06_cycle_03.txt, 06_cycle_04.txt, 06_cycle_05.txt, 06_cycle_06.txt, 07_hand_00.txt, 07_hand_01.txt, 07_hand_02.txt, 07_hand_03.txt, 07_hand_04.txt, 07_hand_05.txt, 07_hand_06.txt, 07_hand_07.txt, 07_hand_08.txt, 07_hand_09.txt, 07_hand_10.txt, 07_hand_11.txt, 07_hand_12.txt, 07_hand_13.txt, 07_hand_14.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 2 ms 3632 KiB
00_sample_01.txt AC 2 ms 3668 KiB
00_sample_02.txt AC 2 ms 3664 KiB
00_sample_03.txt AC 2 ms 3540 KiB
01_small_00.txt AC 1 ms 3444 KiB
01_small_01.txt AC 2 ms 3536 KiB
01_small_02.txt AC 2 ms 3544 KiB
01_small_03.txt AC 1 ms 3488 KiB
01_small_04.txt AC 2 ms 3628 KiB
01_small_05.txt AC 2 ms 3536 KiB
01_small_06.txt AC 2 ms 3520 KiB
01_small_07.txt AC 2 ms 3448 KiB
01_small_08.txt AC 2 ms 3424 KiB
01_small_09.txt AC 1 ms 3516 KiB
01_small_10.txt AC 1 ms 3520 KiB
01_small_11.txt AC 2 ms 3480 KiB
01_small_12.txt AC 2 ms 3592 KiB
01_small_13.txt AC 2 ms 3420 KiB
01_small_14.txt AC 2 ms 3520 KiB
01_small_15.txt AC 2 ms 3628 KiB
01_small_16.txt AC 1 ms 3460 KiB
01_small_17.txt AC 2 ms 3524 KiB
02_dense_00.txt AC 14 ms 8812 KiB
02_dense_01.txt AC 18 ms 10048 KiB
02_dense_02.txt AC 14 ms 9000 KiB
02_dense_03.txt AC 15 ms 9552 KiB
02_dense_04.txt AC 14 ms 9024 KiB
02_dense_05.txt AC 22 ms 11468 KiB
02_dense_06.txt AC 11 ms 7588 KiB
02_dense_07.txt AC 8 ms 6184 KiB
02_dense_08.txt AC 12 ms 7396 KiB
02_dense_09.txt AC 9 ms 6756 KiB
02_dense_10.txt AC 9 ms 6472 KiB
02_dense_11.txt AC 11 ms 7248 KiB
03_rnd_00.txt AC 59 ms 20472 KiB
03_rnd_01.txt AC 22 ms 9400 KiB
03_rnd_02.txt AC 42 ms 16116 KiB
03_rnd_03.txt AC 68 ms 19944 KiB
03_rnd_04.txt AC 50 ms 16024 KiB
03_rnd_05.txt AC 39 ms 13284 KiB
03_rnd_06.txt AC 44 ms 14644 KiB
03_rnd_07.txt AC 56 ms 17704 KiB
04_same_00.txt AC 51 ms 17020 KiB
04_same_01.txt AC 52 ms 16688 KiB
04_same_02.txt AC 67 ms 21272 KiB
04_same_03.txt AC 36 ms 12756 KiB
04_same_04.txt AC 53 ms 20028 KiB
04_same_05.txt AC 57 ms 19940 KiB
04_same_06.txt AC 40 ms 16736 KiB
04_same_07.txt AC 51 ms 20124 KiB
05_star_00.txt AC 60 ms 23104 KiB
05_star_01.txt AC 56 ms 23092 KiB
05_star_02.txt AC 58 ms 23104 KiB
05_star_03.txt AC 58 ms 23100 KiB
05_star_04.txt AC 60 ms 23088 KiB
06_cycle_00.txt AC 70 ms 20752 KiB
06_cycle_01.txt AC 73 ms 20756 KiB
06_cycle_02.txt AC 68 ms 20736 KiB
06_cycle_03.txt AC 74 ms 20244 KiB
06_cycle_04.txt AC 76 ms 20136 KiB
06_cycle_05.txt AC 56 ms 16916 KiB
06_cycle_06.txt AC 58 ms 17004 KiB
07_hand_00.txt AC 43 ms 15228 KiB
07_hand_01.txt AC 43 ms 15308 KiB
07_hand_02.txt AC 37 ms 15240 KiB
07_hand_03.txt AC 37 ms 15384 KiB
07_hand_04.txt AC 53 ms 21448 KiB
07_hand_05.txt AC 58 ms 21000 KiB
07_hand_06.txt AC 46 ms 16028 KiB
07_hand_07.txt AC 58 ms 17468 KiB
07_hand_08.txt AC 63 ms 18588 KiB
07_hand_09.txt AC 63 ms 19272 KiB
07_hand_10.txt AC 70 ms 20056 KiB
07_hand_11.txt AC 70 ms 20220 KiB
07_hand_12.txt AC 67 ms 20316 KiB
07_hand_13.txt AC 60 ms 18620 KiB
07_hand_14.txt AC 58 ms 18552 KiB