上一页 1 ··· 25 26 27 28 29 30 31 32 33 ··· 42 下一页
摘要: 题意:给定一张图,包括n个城市,m条路径,q个询问( 图中没有环 )。bfs会超时!!LCA问题:询问 a,b 之间的最短距离。因为图中不可能出现环,所以两点之间有且仅有一条路,或者没有。所以先预处理出a,b的最近公共祖先,和他们各自的距离。比如a,b的最近公共祖先为father(简易图)aa----father----b | | a1 | aans=dis[ a ]+dis[ b ]-dis[ father ]*2;dfs中的fa==0判断是因为这是无向图。。。谨记。。。这只是求距离,对父子关系要求不明显View Code 1 #incl... 阅读全文
posted @ 2013-02-04 20:02 xxx0624 阅读(833) 评论(0) 推荐(0)
摘要: 2-sat初次接触2-sat。首先对于2-sat问题,可简化为:现在有N个党派,每个党派只有2名人员,从2者挑出一个,且某两个党派挑出来的人可能存在矛盾,这时不能同时选择他们。(引用对称性解决2-sat的ppt)例如:(x1,x2),(y1,y2)为两个不同党派的人。若两两之间都没有矛盾,则do nothing若 x1-y1有矛盾,则他们不能同时被选择,所以 (x1,y2)是必选的,或者(x2,y1)是必选的。然后根据这条规律,tarjan+缩点,得到新的一张图,这张图没有环,若某个“点”中存在(x1,x2)(即:某个党派2个人同时出现在这个“点”中),这是就不能达到目标。反之成立。View 阅读全文
posted @ 2013-02-04 15:20 xxx0624 阅读(535) 评论(0) 推荐(0)
摘要: 例如:6 a b a b a b ‘\0’下标i: 0 1 2 3 4 5 6Next: -1 0 0 1 2 3 4dp: 0 1 1 2 2 3 3dp[ 1 ]=1表示目前:‘a’出现一次dp[ 2 ]=1:同上:“ab”dp[ 3 ]=2表示“abab”一次,“ab”一次。。。dp[ 6 ]=3表示首字母‘a’在整个的出现次数KMP够强大!!!!!View Code 1 #include<stdio.h> 2 #include<string.h> 3 const int maxn = 200005; 4 const int mod= 10007; 5 char 阅读全文
posted @ 2013-02-03 15:53 xxx0624 阅读(262) 评论(0) 推荐(0)
摘要: 题意:一个字符串在另一个字符串中出现的次数View Code 1 #include<stdio.h> 2 #include<string.h> 3 const int maxn = 10005; 4 const int maxm= 1000005; 5 int lena,lenb; 6 char a[ maxm ],b[ maxn ]; 7 int next[ maxn ]; 8 int ans; 9 void getNext(){10 int j,k;11 j=0,k=-1;12 next[0]=-1;13 while( j<lenb ){14 ... 阅读全文
posted @ 2013-02-03 00:02 xxx0624 阅读(277) 评论(0) 推荐(0)
摘要: floyd新应用!!!!View Code 1 #include 2 const int maxn = 101; 3 const int inf = 9999999; 4 int n,m; 5 int dis[ maxn ][ maxn ],mat[ maxn ][ maxn ]; 6 7 void floyd(){ 8 int ans=inf; 9 for( int i=1;i( dis[i][j]+mat[i][k]+mat[k][j] ) )17 ans=dis[i][j]+mat[i][k]+mat[k][j];18 ... 阅读全文
posted @ 2013-02-02 23:32 xxx0624 阅读(265) 评论(0) 推荐(0)
摘要: View Code 1 #include<stdio.h> 2 #include<stack> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int maxn = 10005; 8 const int maxm = 100005; 9 struct node{10 int u,next;11 }edge[ maxm ];12 int head[ maxn ],cnt;13 int vis[ m 阅读全文
posted @ 2013-02-02 22:43 xxx0624 阅读(210) 评论(0) 推荐(0)
摘要: 因为可以通过其他人来通知他们认识的人,所以这幅图可以用强连通分量变成一个 缩点的图,所有相互强连通分支变成一个缩点,求的所有缩点中入度为0的缩点即为 所求的需要通知的最小人数。然后再枚举所有人所在的缩点是否入度为0,是的话更 新该缩点所需的最小费用即可View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<stack> 5 #include<algorithm> 6 using namespace std; 7 const i 阅读全文
posted @ 2013-02-02 22:15 xxx0624 阅读(1606) 评论(0) 推荐(0)
摘要: ANS=0;if x*x+x*x==n ANS+=4;//( x,x) (-x,-x) (-x,x) (x,-x)if x*x+0*0==n ANS+=4;//(x,0) (0,x) (-x,0) (0,-x)if a*a+b*b==n ANS+=8;View Code 1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 const int maxn = 10005; 5 int a[ maxn ]; 6 int main(){ 7 memset( a,0,sizeof(a)); 8 a 阅读全文
posted @ 2013-02-02 19:57 xxx0624 阅读(222) 评论(0) 推荐(0)
摘要: 题意:汽车有两个标量:里程,油量。给定一些数,若不加油进去,里程逐渐增大,油量减少;但若加油了,油量则会突然增大,这组数据应该放弃不用。然后求出平均速度,再用最后油量求出能走多远。View Code 1 #include<stdio.h> 2 #include<string.h> 3 const int maxn = 1005; 4 struct node{ 5 double dis,fuel; 6 }a[ maxn ]; 7 int main(){ 8 int n=0; 9 double aa,bb;10 while( scanf("%lf%lf" 阅读全文
posted @ 2013-02-02 11:11 xxx0624 阅读(250) 评论(0) 推荐(0)
摘要: View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<queue> 5 using namespace std; 6 const int maxn = 21; 7 const int inf = INT_MAX; 8 int r[ maxn ],c[ maxn ],sd[ maxn ],md[ maxn ];//sd=r+c,md=r-c+n-1; 9 int ans,n;10 void init(){11 memset( r,0,si 阅读全文
posted @ 2013-02-01 22:25 xxx0624 阅读(278) 评论(0) 推荐(0)
上一页 1 ··· 25 26 27 28 29 30 31 32 33 ··· 42 下一页