zju 2370 Expanding Rods 【解题报告】

Galaxy War

Time limit: 5 Seconds   Memory limit: 32768K  
Total Submit: 25   Accepted Submit: 7  

In order to count the monsters from another dimension, many stars in galaxy joint the Association Counter Monsters (ACM). They built many bidirectional tunnels to exchange messages. They built some tunnels so that any circle in the graph formed by the tunnel network, whose length is larger than 3, had at least one chord. A chord is an edge connecting two non-adjacent points in the cycle.

One day, the Chief of ACM asked you to draw a map of the stars in ACM. In the map, the two endpoint of any tunnel must have different colors. For security reason, the more methods of drawing the map, the better. So the Chief asked you how many methods there were if he provided you n colors.

Given you all the tunnels among the stars, your task is to answer the Chief's question as soon as possible.

Input

The input contains several test cases.

For each case, the first line contains two integers n (2 <= n <= 1000), m, indicating the number of stars in ACM and the number of tunnels. The following m lines contain two integers each, indicating the stars that tunnel connects. The stars are numbered from 1 to n. No tunnel connects a star to itself, and no two stars are connected with more than one tunnel. Then an integer P (0 < P <= 1000), the number of the Chief's questions, follows. The following P lines contain 2 integers C (0 < C <= 1000000000), M (0 < M <= 1000000000), each, meaning that the Chief provides you C colors, and that you should output the number of methods module M.

Output

For each quest, output the answer in one line.

Sample Input

 

4 5
1 2
2 3
3 4
4 1
1 3
2
2 10
3 10

 

Sample Output

 

0
6

 

HINT

The graph with the property mentioned above is called chordal graph. A permutation s = [v1 , v2 ,..., vn] of the vertices of such graph is called a perfect elimination order if each vi is a simplicial vertex of the subgraph of G induced by { vi ,..., vn}. A vertex is called simplicial if its adjacency set induces a complete subgraph, that is, a clique (not necessarily maximal). The perfect elimination order of a chordal graph can be computed with the following codes:

procedure maximum cardinality search(G, s)
for all vertices v of G do
set label[v] to zero
end for
for all i from n downto 1 do
choose an unnumbered vertex v with largest label
set s(v) to i{number vertex v}
for all unnumbered vertices w adjacent to vertex v do
increment label[w] by one
end for
end for
end procedure

 

Author: GUAN, Yao

------------------
ps:这个题目的关键全在于后面给出的Hint,当然知道这个定理也就不算什么题目了。
对于弦图,在完美削去顺序的前提下,每一个点加上后面与之相邻的点所构成的整个图的一个子图为完全图。
那么答案也就非常显然了,即为pi{degree[vi]}。

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long int64;

const int cMaxN = 1100;

int n, m;
bool edge[cMaxN] [cMaxN];
bool mk[cMaxN];
int PEO[cMaxN], degree[cMaxN];

bool init();
void solve();

int main()
{
  while(init()) {
    solve();
  }
}

bool init()
{
  if (2!=scanf("%d%d", &n, &m)) return false;
  int a, b;
  int i, j, k;
  memset(edge, 0, sizeof(edge));
  for (i = 0; i< m; i++) {
    scanf("%d%d", &a, &b);
    a--, b--;
    edge[a][b] = edge[b][a] = true;
  }
  memset(degree, 0, sizeof(degree));
  memset(mk, 0, sizeof(mk));
  for (i = n-1; i>=0; i--) {
    for (j = 0, k = -1; j<n; j++) if (!mk[j])
      if (k==-1||degree[j]>degree[k]) k = j;
    mk[k] = true, PEO[i] = k;
    for (j = 0; j<n; j++) if (!mk[j]&&edge[k][j])
      degree[j]++;
  }
  return true;
}

void solve()
{
  int q;
  int64 C, M, ans;
  //  for (int i = 0; i<n; i++) printf("%d ", PEO[i]); printf("\n");
  //  for (int i = 0; i<n; i++) printf("%d ", degree[PEO[i]]); printf("\n");
  for (scanf("%d", &q); q>0; q--) {
    cin>>C>>M;
    ans = 1 % M;
    for (int i = 0; i<n; i++) {
      if (degree[PEO[i]]>C) ans = 0;
      ans = ans * (C-degree[PEO[i]]) % M;
    }
    cout<<ans<<endl;
  }
}

/*
  2429508 2007-05-13 11:02:24 Accepted 2841 C++ 00:01.62 2036K AndyZhau
*/


posted @ 2008-12-05 15:08  jesonpeng  阅读(279)  评论(0)    收藏  举报