摘要:
并查集。781MS,水过。。。代码: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int set[100002], mark[100002]; 7 int flag; 8 9 int find(int a) // 查找a的根结点10 {11 int x=a;12 while(set[x] != x)13 x=set[x];14 return x;15 }16 17 void merge(int a, int b) //合并a和b都的根结点18 {19...
阅读全文
posted @ 2012-08-03 15:32
zrq495
阅读(121)
推荐(0)
摘要:
并查集算法。 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int cnt; //统计最少要添加的道路 7 int set[1001]; 8 9 int find(int a) //查找a的根结点10 {11 int x=a;12 while(set[x] != x)13 x=set[x];14 return x;15 }16 17 void merge(int a, int b) //合并a和b的根结点18 {19 int x...
阅读全文
posted @ 2012-08-03 11:14
zrq495
阅读(125)
推荐(0)
摘要:
判断顶点入度是否唯一即可。如果入度为0的节点只有一个,输出Yes,否则输出No。代码: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int len; 7 int map[1001][1001]; 8 char name[1001][100]; 9 10 int func(char str[])11 {12 int i;13 for (i=0; i<len; i++)14 {15 if (strcmp(name[i], str)==0)16 ...
阅读全文
posted @ 2012-08-02 18:41
zrq495
阅读(161)
推荐(0)
摘要:
拓扑排序。 (1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它. (2)从网中删去该顶点,并且删去从该顶点发出的全部有向边. (3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.代码如下: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int main() 7 { 8 int i, j, k, n, m, a, b; 9 int dgr[501], map[501][501]; //dgr[]存放节点的入度10 while(ci...
阅读全文
posted @ 2012-08-02 18:23
zrq495
阅读(463)
推荐(0)
摘要:
map[i][j]=1 表示第i 个顶点指向第j个顶点;如果map[1][i]=1, map[i][j]=1, 则map[1][j]=1。代码如下;#include<iostream>#include<cstring>using namespace std;int map[100][100];int main(){ int i, j; char str[1000]; while(cin >> str) { while (strcmp(str, "0")) { int len=strlen(str); ...
阅读全文
posted @ 2012-08-01 21:08
zrq495
阅读(180)
推荐(0)
摘要:
DFS代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int m, n; 6 char map[102][102]; 7 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; //标记8个方向 8 9 void dfs(int x, int y)10 {11 int i, xx, yy;12 for (i=0; i<8; i++) //扫描8个方向13 {14 ...
阅读全文
posted @ 2012-08-01 11:05
zrq495
阅读(230)
推荐(0)
摘要:
题意:给出一个进制,一个数的最低位,和另外的一个数,比如10进制,第一个数字的最低位是7,第二个数字是4,和规则(XXXXX7 * 4 = 7XXXXX,例子: 179487 * 4 = 717948)求出第一个数字的最小长度。看起来很难,其实动笔写写就明白了。输入k,m,n,原来的数字为s,因为s的最后一位为m,则s*n的最后一位为s*n%k,而s*n%k又是s的倒数第二位,这样又可以计算出ans*n的倒数第二位;以此类推,直到乘积+原来的进位==最低位。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int mai
阅读全文
posted @ 2012-07-31 20:06
zrq495
阅读(532)
推荐(0)
摘要:
设n=a*b,则a趟去的时候打开灯,b趟去的时候关上,相互抵消,当且仅当a=b时,第n个灯只开不关,即n为完全平方数。代码如下: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int main() 7 { 8 long long n, m; 9 while(cin >> n, n)10 {11 m=sqrt(n);12 if (m*m == n) cout << "yes" << endl;13 else cout <&
阅读全文
posted @ 2012-07-31 17:36
zrq495
阅读(227)
推荐(0)
摘要:
看懂上图就出题了,注意给出的数字很长,可以用字符数组存放 。代码如下: 1 #include<iostream> 2 #include<cmath> 3 #include<cstring> 4 5 using namespace std; 6 7 int main() 8 { 9 int n, s;10 int i;11 char str[40];12 while(cin >> str, strcmp(str, "0"))13 {14 s=0;15 n=strlen(str);16 for (i=0; ...
阅读全文
posted @ 2012-07-31 16:56
zrq495
阅读(187)
推荐(0)
摘要:
1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int prime(int n) 7 { 8 int i; 9 for (i=2; i<sqrt(n)+1; i++)10 {11 if (n%i == 0) return 0;12 }13 return 1;14 }15 16 int main()17 {18 int n, i;19 int cnt;20 while(cin >> n, n)21 {22 ...
阅读全文
posted @ 2012-07-31 14:54
zrq495
阅读(152)
推荐(0)
摘要:
题意:给一个 p 和 一个 a,如果这个p 本身就是一个素数,就输出 no,如果不是素数,那么计算( a ^ p) % p 如果结果等于 a 那么输出 yes 否则输出 no。快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是(a*b) mod c=(a mod c)*(b mod c)mod c。代码如下: 1 #include<iostream> 2 #include<cmath> 3 4 using namespace std; 5 6 int prime(long long n) 7 { 8 int i; 9 for (i=2; i<
阅读全文
posted @ 2012-07-31 14:51
zrq495
阅读(153)
推荐(0)
摘要:
错排: 当n个编号元素放在n个编号位置, 元素编号与位置编号各不对应的方法数用M(n)表示, 那么M(n-1)就表示n-1个编号元素放在n-1个编号位置, 各不对应的方法数, 其它类推. 第一步, 把第n个元素放在一个位置, 比如位置k,一共有n-1种方法; 第二步,放编号为k的元素,这时有两种情况. 1,把它放到位置n,那么,对于剩下的n-2个元素,就有M(n-2)种方法; 2,不把它放到位置n,这时,对于这n-2个元素,有M(n-1)种方法; 综上得到 M(n)=(n-1)[M(n-2)+M(n-1)] 特殊地,M(1)=0,M(2)=1最小的几个错排数是:D...
阅读全文
posted @ 2012-07-31 14:23
zrq495
阅读(167)
推荐(0)
摘要:
1 #include<iostream> 2 3 using namespace std; 4 5 int gcd(int a, int b) 6 { 7 return b?gcd(b, a%b):a; 8 } 9 10 int main()11 {12 int a, b, c, d, m, k;13 int n;14 cin >> n;15 while(n--)16 {17 cin >> a >> b >> c >> d;18 int t=gcd(b,d);19 m=a*d/t+b*c/t;...
阅读全文
posted @ 2012-07-31 14:19
zrq495
阅读(115)
推荐(0)
摘要:
求a/b与c/b的最小公倍数。要求最小公倍数,那么结果肯定是分子尽量小,即 求a , c 的最小公倍数, 分母尽量大, 即求 b , d 的最大公约数。、求最大公约数:int gcd(int a, int b){ return b?gcd(b, a%b):a;}代码: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int gcd(int a, int b) 7 { 8 return b?gcd(b, a%b):a; 9 }10 11 int lcm(int a, int b)12
阅读全文
posted @ 2012-07-31 14:13
zrq495
阅读(285)
推荐(0)