<pre name= "code" class= "CPP" >
Idea: The most violent idea, with a triple loop to traverse, the problem gives the time is 2 seconds, and the CF server must be very good, so time should be no pressure, but my heart has been no end, the total feeling will time out.
Here, look at the code of Daniel, learn a new idea, for a triple loop, you can find out what the relationship between their 22, with pair, or strcut to save, the topic of M is 4000, far less than N, this will make time pressure to reduce a lot,
62ms
#include <bits/stdc++.h>using namespace std;const int Inf=0x3f3f3f3f;const int Maxn=5005;int g[maxn][maxn];int Degree[maxn];int Main (void) { int n,m; scanf ("%d%d", &n,&m); memset (degree,0,sizeof (degree)); memset (G,0,sizeof (g)); for (int i=1;i<=m;i++) { int a, b; scanf ("%d%d", &a,&b); G[a][b]=1; G[b][a]=1; degree[a]++; degree[b]++; } int minn=inf; for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) { if (G[i][j]) {for (int k=j+1 ; k<=n;k++) { if (G[i][k]&&g[j][k]) { minn=min (Minn,degree[i]+degree[j]+degree[k] -6) ; }}}} if (minn!=inf) printf ("%d\n", Minn); else printf (" -1\n"); return 0;}
31ms
#include <bits/stdc++.h>using namespace std;const int Inf=0x3f3f3f3f;const int Maxn=5005;int g[maxn][maxn];int Degree[maxn];int Main (void) { int n,m; scanf ("%d%d", &n,&m); memset (degree,0,sizeof (degree)); memset (G,0,sizeof (g)); for (int i=1;i<=m;i++) { int a, b; scanf ("%d%d", &a,&b); G[a][b]=1; G[b][a]=1; degree[a]++; degree[b]++; } int minn=inf; for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) { if (G[i][j]) {for (int k=j+1 ; k<=n;k++) { if (G[i][k]&&g[j][k]) { minn=min (Minn,degree[i]+degree[j]+degree[k] -6) ; }}}} if (minn!=inf) printf ("%d\n", Minn); else printf (" -1\n"); return 0;}
Copyright NOTICE: Reprint must please indicate the source, thank you
Codeforces Round #318 (Div. 2)