sicily 1024 Magic Island

深搜中与记录长度有关的题目,都差不多是这样的!

还有邻接表的写法,不够熟!

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 int ans;
 6
 7 bool vis[10005];
 8
 9 struct edge {
10     int u;
11     int v;
12     int l;
13 };
14
15 vector <edge> graph[10005];
16
17 void dfs(int n, int x, int temp)
18 {
19     if(ans < temp)
20         ans=temp;
21     vis[x]=1;
22     for(int i=0; i<graph[x].size(); i++)
23     {
24         if(!vis[graph[x][i].v])
25             dfs(n, graph[x][i].v, temp+graph[x][i].l);
26     }
27 }
28
29
30 int main()
31 {
32     int n,k;
33     while(scanf("%d%d", &n, &k) != EOF)
34     {
35         ans=0;
36         int temp=0;
37         memset(graph, 0, sizeof(graph));
38         memset(vis, 0, sizeof(vis));
39         int u,v,l;
40         for(int i=0; i<n-1; i++)
41         {
42             edge e;
43             scanf("%d%d%d", &u, &v, &l);
44             e.u = u;
45             e.v = v;
46             e.l = l;
47             graph[u].push_back(e);    //注意是双向的啊!!!
48             e.u = v;
49             e.v = u;
50             e.l = l;
51             graph[v].push_back(e);
52         }
53
54         dfs(n, k, temp);
55         printf("%d\n", ans);
56     }
57     return 0;
58 } 
时间: 2024-12-18 15:41:55

sicily 1024 Magic Island的相关文章

SOJ 1024. Magic Island

题目大意:给定一个n个点n-1条的连通无向图,求从任意一点出发,在不重复经过同一点的情况下,所能走过的的边的最大权值. 解题思路:深度优先搜索. 1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 struct Edge { 6 int to; 7 int dis; 8 Edge(int t = 0, int d = 0) { 9 to = t; 10 dis = d; 11 } 12 }; 13

Sicily 1302 Magic Square (数论)

链接:http://soj.me/1302 题目中有图和表格,所以就不把题目信息列出来了,打开链接直接看就行了... 分析: 从填幻方的过程,可以判断一下几点: 1>.幻方的每一列都是向下发展的: 2>.填入的前n个数都刚好是每一列第一个填入的数: 从这两点可以推出下面几点: (1). 每一列的发展是平面的(即每一列的个数是一样的,特别的,只有一列少一): (2). 右下角那个数肯定是填入的最大的: (3). 当我们填入到右下角的时候,每一列的个数都刚好是n/2(注意n是奇数,这里的除是整除)

sicily 1024 邻接矩阵与深度优先搜索解题

Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know

sicily1024 Magic Island(图的遍历)

Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

Problem A CodeForces 560A

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone

货币问题

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect an

货币体系

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone