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 76 77 78 79 80 81 82 83 84 85 86 87
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=2005;
vector<int>edges[maxn]; int n,m,cnt[maxn][maxn],from[maxn][2],dist[maxn][2],map[maxn][maxn],ans[maxn];
void AddEdge(int x,int y) { edges[x].push_back(y); }
struct QueNode { int u,type; QueNode(int a=0,int b=0):u(a),type(b) {} };
void Bfs(int s) { memset(from,0,sizeof(from)); dist[s][0]=dist[s][1]=0; queue<QueNode>Q; for(int Next:edges[s]) { dist[Next][0]=1; from[Next][0]=Next; Q.push(QueNode(Next,0)); } while(!Q.empty()) { QueNode Now=Q.front(); Q.pop(); for(int Next:edges[Now.u]) { if(!from[Next][0]) { from[Next][0]=from[Now.u][Now.type]; dist[Next][0]=dist[Now.u][Now.type]+1; Q.push(QueNode(Next,0)); } else if(!from[Next][1]&&!map[Next][s]&&from[Now.u][Now.type]!=from[Next][0]) { from[Next][1]=from[Now.u][Now.type]; dist[Next][1]=dist[Now.u][Now.type]+1; Q.push(QueNode(Next,1)); } } } for(int mid:edges[s]) for(int Next:edges[mid]) if(Next!=s&&from[Next][0]==mid)ans[mid]+=dist[Next][1]-dist[Next][0]; }
int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=m; i++) { int x=Get_Int(),y=Get_Int(); AddEdge(x,y); AddEdge(y,x); map[x][y]=map[y][x]=1; } for(int Now=1; Now<=n; Now++) for(int mid:edges[Now]) for(int Next:edges[mid]) if(Next!=Now)cnt[Now][Next]++; for(int i=1; i<=n; i++) if(count(cnt[i]+1,cnt[i]+n+1,1))Bfs(i); for(int i=1; i<=n; i++)printf("%d\n",ans[i]/2); return 0; }
|