1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include<stdio.h> #include<set> #include<iostream> #include<stack> #include<cstring> #include<cmath> #include<vector> #include<algorithm> typedef long long ll; typedef long double ld; typedef double db; #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int mod=998244353; int mo(ll a,int p){ return a>=p?a%p:a; } inline int rd() { int x = 0, f = 1; char ch; while (ch < '0' || ch > '9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } int n,m,f[120],ans; int a[120]; int find(int x){ return f[x]==0?x:f[x]=find(f[x]); } struct E{ int u,v,w; }e[220];; int cmp(E x,E y){ return x.w<y.w; } int main(){ io_opt; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } int x,y; for(int i=1;i<=m;i++){ cin>>x>>y; if(a[x]==a[y]){ e[i]=(E){x,y,1}; } else{ e[i]=(E){x,y,0}; } } sort(e+1,e+1+m,cmp); int cnt=0; for(int i=1;i<=m;i++){ int u=find(e[i].u),v=find(e[i].v); if(u!=v){ f[u]=v; cnt++; if(e[i].w) ans++; } if(cnt==n-1) break; } cout<<ans<<endl; return 0; }
|