#include <bits/stdc++.h>
using namespace std;
const int SIZE=500000;
void imax(int &a, int b){
a=max(a, b);
}
void imin(int &a, int b){
a=min(a, b);
}
void lmax(long long &a, long long b){
a=max(a, b);
}
void lmin(long long &a, long long b){
a=min(a, b);
}
/*
WARNING: I'm using strange bracket style!
*/
class findUnion{
int p[SIZE];
public:
void clear(int n){
for (int i=0; i<=n; i++)
p[i]=i;
}
int Find(int u){
return (p[u]==u)?p[u]:p[u]=Find(p[u]);
}
void Union(int x, int y){
p[Find(x)]=Find(y);
}
};
findUnion BUSH;
vector <pair <int, int> > edges;
int n, q, x, y;
long long res;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
while (q--)
cin>>x>>y, edges.push_back({x, y});
for (int i=0; i<edges.size(); i++)
for (int j=i+1; j<edges.size(); j++)
for (int g=j+1; g<edges.size(); g++)
{
BUSH.clear(n), x=n;
for (int h=0; h<edges.size(); h++)
if (h!=i && h!=j && h!=g && BUSH.Find(edges[h].first)!=BUSH.Find(edges[h].second))
BUSH.Union(edges[h].first, edges[h].second), x--;
if (x!=1)
res++;
}
cout<<res<<endl;
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; const int SIZE=500000; void imax(int &a, int b){ a=max(a, b); } void imin(int &a, int b){ a=min(a, b); } void lmax(long long &a, long long b){ a=max(a, b); } void lmin(long long &a, long long b){ a=min(a, b); } /* WARNING: I'm using strange bracket style! */ class findUnion{ int p[SIZE]; public: void clear(int n){ for (int i=0; i<=n; i++) p[i]=i; } int Find(int u){ return (p[u]==u)?p[u]:p[u]=Find(p[u]); } void Union(int x, int y){ p[Find(x)]=Find(y); } }; findUnion BUSH; vector <pair <int, int> > edges; int n, q, x, y; long long res; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>q; while (q--) cin>>x>>y, edges.push_back({x, y}); for (int i=0; i<edges.size(); i++) for (int j=i+1; j<edges.size(); j++) for (int g=j+1; g<edges.size(); g++) { BUSH.clear(n), x=n; for (int h=0; h<edges.size(); h++) if (h!=i && h!=j && h!=g && BUSH.Find(edges[h].first)!=BUSH.Find(edges[h].second)) BUSH.Union(edges[h].first, edges[h].second), x--; if (x!=1) res++; } cout<<res<<endl; return 0; } |
English