关于hdu2196Computer【树形DP】【换根法】和树根的更换的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于3DComputerGrapihcsUsingOpenGL-18相机移动
关于hdu2196 Computer【树形 DP】【换根法】和树根的更换的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于3D Computer Grapihcs Using OpenGL - 18 相机移动、A Self-Organized Computer Virus Demo in C、An Introduction to Computer Thinking、CE235 Computer Security等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- hdu2196 Computer【树形 DP】【换根法】(树根的更换)
- 3D Computer Grapihcs Using OpenGL - 18 相机移动
- A Self-Organized Computer Virus Demo in C
- An Introduction to Computer Thinking
- CE235 Computer Security
hdu2196 Computer【树形 DP】【换根法】(树根的更换)
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34390 Accepted Submission(s): 5383

Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
题意:
一棵有 n 个节点的树,有权边。问每个节点到其他节点的最远距离分别是多少。
思路:
如果固定一个节点,那么 dfs 找最远的距离就可以了。用 dp [i] 表示以 i 为根的子树中,i 可到的最远距离。
对于任意一个节点 i,他到达的最远节点有可能在子树中,也有可能不在子树中。所以我们分别找到子树中的最远距离和不在子树中的最远距离。
dfs1 就是用 dp 存储了在子树中的最远距离。
dfs2 就是把当前节点换到了根的位置,用 f 存储不在子树中的最远距离。那么他到他子树节点的距离是变短了的就不用管了。
把 i 换成根,把 i 的父亲 father 换下去,如果 i 在 i 的父亲这棵子树的最长路径上。father 换下去之后,就要考虑原本以 father 为根的子树的第二长路径。
如果 i 不在 father 的最长路径上,那就是在 f 和 dp 中找大的加上当前路径长度。
1 //#include <bits/stdc++.h>
2 #include<iostream>
3 #include<cmath>
4 #include<algorithm>
5 #include<stdio.h>
6 #include<cstring>
7 #include<vector>
8 #include<map>
9 #include<set>
10
11 #define inf 0x3f3f3f3f
12 using namespace std;
13 typedef long long LL;
14
15 const int maxn = 10005;
16 int head[maxn], dp[maxn], f[maxn], second[maxn], longest[maxn];
17 struct node{
18 int v;
19 int nxt;
20 int weight;
21 }edge[maxn * 2];
22 int n, cnt;
23
24 void addedge(int u, int v, int w)
25 {
26 edge[cnt].v = v;
27 edge[cnt].weight = w;
28 edge[cnt].nxt = head[u];
29 head[u] = cnt++;
30 edge[cnt].v = u;
31 edge[cnt].weight = w;
32 edge[cnt].nxt = head[v];
33 head[v] = cnt++;
34 }
35
36 void dfs1(int rt, int fa)
37 {
38 for(int i = head[rt]; i != -1; i = edge[i].nxt){
39 int son = edge[i].v;
40 if(son == fa)continue;
41 dfs1(son, rt);
42 //dp[rt] = max(dp[rt], dp[son] + edge[i].weight);
43 if(dp[son] + edge[i].weight > dp[rt]){
44 longest[rt] = son;
45 second[rt] = max(second[rt], dp[rt]);
46 dp[rt] = dp[son] + edge[i].weight;
47 }
48 else if(second[rt] < dp[son] + edge[i].weight){
49 second[rt] = dp[son] + edge[i].weight;
50 }
51 }
52 }
53
54 void dfs2(int rt, int fa)
55 {
56 for(int i = head[rt]; i != -1; i = edge[i].nxt){
57 int son = edge[i].v;
58 if(son == fa)continue;
59 if(longest[rt] == son)f[son] = max(f[rt], second[rt]) + edge[i].weight;
60 else f[son] = max(f[rt], dp[rt]) + edge[i].weight;
61 dfs2(son, rt);
62 }
63 }
64
65 int main(){
66 while(scanf("%d", &n) != EOF){
67 cnt = 0;
68 memset(head, -1, sizeof(head));
69 for(int i = 2; i <= n; i++){
70 int u, w;
71 scanf("%d%d", &u, &w);
72 addedge(u, i, w);
73 }
74 memset(dp, 0, sizeof(dp));
75 memset(second, 0, sizeof(second));
76 memset(longest, 0, sizeof(longest));
77 memset(f, 0, sizeof(f));
78 dfs1(1, 0);
79
80 dfs2(1, 0);
81 //f[1] = dp[1];
82 for(int i = 1; i <= n; i++){
83 printf("%d\n", max(dp[i], f[i]));
84 }
85 }
86 return 0;
87 }
3D Computer Grapihcs Using OpenGL - 18 相机移动
移动相机需要用到键盘按键,按键事件的引入需要包含头文件
#include <Qt3DInput\qkeyevent.h>
并实现 QWidget 中定义的虚函数 keyPressEvent
我们首先在 MyGlWindow 中重写这个虚函数。
在 MyGlWindow.h 加入
void keyPressEvent(QKeyEvent*);
在 MyGlWindow.cpp 中定义:
1 void MyGlWindow::keyPressEvent(QKeyEvent * e)
2 {
3 switch (e->key())
4 {
5 case Qt::Key::Key_W:
6 camera.moveForward();
7 break;
8 case Qt::Key::Key_S:
9 camera.moveBackward();
10 break;
11 case Qt::Key::Key_A:
12 camera.strafeLeft();
13 break;
14 case Qt::Key::Key_D:
15 camera.strafeRight();
16 break;
17 case Qt::Key::Key_Q:
18 camera.moveUp();
19 break;
20 case Qt::Key::Key_E:
21 camera.moveDown();
22 break;
23
24 default:
25 break;
26 }
27 repaint();
28 }
代码很简单,从中我们可以看到按下 W 和 S 键会分别前进、后退,按下 A 和 D 键会分别向左向右平移,按下 Q 和 E 键会分别向上向下平移。
但是目前我们还没有在 Camera 类中实现这 6 个移动的函数。
下面就来实现这些函数:
在 Camera.h 中添加成员:
1 void moveForward();
2 void moveBackward();
3 void strafeLeft();
4 void strafeRight();
5 void moveUp();
6 void moveDown();
7
8 float moveSpeed = 0.1f;
在 Camera.cpp 中定义这些方法:
1 void Camera::moveForward()
2 {
3 position += viewDirection * moveSpeed;
4 }
5
6 void Camera::moveBackward()
7 {
8 position -= viewDirection * moveSpeed;
9 }
10
11 void Camera::strafeLeft()
12 {
13 glm::vec3 pitchAxis = glm::cross(viewDirection, UP);
14 position += pitchAxis * moveSpeed;
15 }
16
17 void Camera::strafeRight()
18 {
19 glm::vec3 pitchAxis = glm::cross(viewDirection, UP);
20 position -= pitchAxis * moveSpeed;
21
22 }
23
24 void Camera::moveUp()
25 {
26 position += UP * moveSpeed;
27 }
28
29 void Camera::moveDown()
30 {
31 position -= UP * moveSpeed;
32 }
编译运行,我们发现相机可以前进后退上下左右平移了。
PS:
这里的上下并不是依照相机的局部坐标的上下,而是世界坐标的上下。要改成局部坐标的上下有些麻烦。
我做了一些尝试,定义一个新的成员 cameraUp,用来表示相机的上方,每次旋转过后都重新使用向量差乘来重新计算 cameraUp。这样确实可以按照相机的上下移动,但是带来了一些其他问题,相机在世界中不再是 “正” 的了,经过几次旋转,会累积一些偏差,导致相机倾斜。这里需要解决这个问题可能需要用到四元数,但是暂时不再深究了。
A Self-Organized Computer Virus Demo in C
A Program that can modify herself and copy herself to somewhere else and execute it. Just like gene-transformable virus.
Here''s the sample code.
#include "stdio.h"
#include "windows.h"
#define MAX_LOOP 10000
int main(int argc, const char** argv)
{
/*** THE VIRUS LOGIC PART ***/
//GENE_MARK
int a = 0;
printf("GENE PRINT:%d\n", a);
/*** THE VIRUS LOGIC PART ***/
// FILE NAME
char file_name[30] = "test";
// MAKE A COPY OF HERSELF
FILE* source = fopen("test.c", "r");
FILE* descendant = fopen("descendant.c", "w+");
printf("SOURCE FILE OPEN RESULT IS : %d \n", (int)source);
printf("DESCENDANT FILE CREATED: %d \n", (int)descendant);
if(descendant==NULL)
{
printf("ERROR ON CREATING DESCENDANT.\n");
return -1;
}
char buff[100] = {0};
// REPLACE GENE MARK PROGRAM
char letter = 0;
// GENE LINE
int idx = 0;
int loop = 0;
int buff_idx = 0;
while(!feof(source))
{
// ALARM
if(loop>MAX_LOOP)
break;
loop ++;
fread(&letter, sizeof(char), 1, source);
buff[buff_idx] = letter;
buff_idx ++;
if(letter==''\n'')
{
if(idx==9)
{
// TRANSFORM GENE
memset(buff, 0, 100);
buff_idx = 0;
strcat(buff, "int a = 1;\n");
}
fwrite(buff, sizeof(char), strlen(buff), descendant);
// CLEAR BUFFER
memset(buff, 0, 100);
buff_idx = 0;
idx ++;
}
}
// DEAL WITH LEFT LETTERS IN BUFFER
if(strlen(buff)>0)
{
strcat(buff, "\n");
fwrite(buff, sizeof(char), strlen(buff)+1, descendant);
}
// CLOSE ALL FILES
fclose(source);
fclose(descendant);
// until the descendant file is written over
/*** COMPILE HERSELF ***/
char* source_file = "descendant.c";
char* dest_file = "descendant.exe";
char command[100] = {0};
strcat(command, "gcc -o ");
strcat(command, dest_file);
strcat(command, " ");
strcat(command, source_file);
// COMPILATION
system(command);
/***********************/
printf("COPYING MYSELF DONE.\n");
printf("WAITING FOR NEXT INSTRUCTION...\n");
char cmd = getchar();
if(cmd==''Y'')
{
printf("BEGIN EXECUTE THE COPYFILE EXECUTION...\n");
//GENE_MARK
system("descendant.exe");
printf("EXECUTION PROCESS IS ACTIVATED, TASK DONE. EXIT SYSTEM.");
}
else
printf("YOU CHOOSE TO EXIT SYSTEM. BYE!");
return 0;
}
If you have any suggestions or ideas, please feel free comment below, thanks!
An Introduction to Computer Thinking
1.Die Grundlage des Computers
1.1 Binärzahl in die Dezimalzahl umsetzen
Bereiten nach Gewicht,dann bekommen Summe
1.2 Dezimalzahl in die Binärzahl umsetzen
Durch 2 Rest bekommen,bis der Quotient 0 ist,liest man von unten nach oben
Integral:
Dezimale:Mal 2 Integral bekommen,bis Dezimaleteil 0 ist,liest man von oben nach unten
1.3 Binärzahl in die Oktalzahl umsetzen
Drei Binärzahl schliessen ein Oktalzahl zusammen.
Integralteil:von Rechts nach Links,drei Zahl in eine Gruppe,falls Zahl nicht genug,fugen 0 hinzu.
Dezimaleteil:von Links nach Rechts,drei Zahl in eine Gruppe,falls Zahl nicht genug,fugen 0 hinzu.
1.4 Oktalzahl in die Binärzahl umsetzen
Eine Oktalzahl teilt in drei Binärzahl ein
1.5 Ursprünglicher Code
Man erklart Zeichen mit Hohe Position.
1.6 Inverse Code
Der inverse Code der positve Zahl ist gleich wie Ursprünglicher Code;Zeichenposition der negative Zahl ist 1,berechnen die restlich Position umgekehrt(1->0 order 0->1)
1.7 Ergänzungscode
Der Ergänzungscode der positve Zahl ist gleich wie ursprünglicher Code und inverse Code;Zeichenposition der negative Zahl ist 1;Der inverse Code plus 1,dann erlangen wir Ergänzungscode.
CE235 Computer Security
CE235 Computer Security Assignment 3
The assignment is worth 7.5% of the total mark for this module. This assignment involves writing a C/C++ program for task 1 described below. The purpose of this assignment is for you to get a good understanding of the SHA-256.
The task:
Task 1 (7.5%)
Read the description and the pseudocode of SHA-256 at https://en.wikipedia.org/wiki/SHA-2 and develop the program called ConsoleSHA.cpp (which will be later renamed to CE235A3.cpp). Your program needs to be able to take an input and show the resulting hash value.
The program should contain brief comments describing the main block of the program, and the program should be laid out neatly with consistent indentation.
Assessment criteria:
Correctness of program 50%
Quality of program 25%
Clarity of program and comments 25%
Submission
You are required to submit the source code files (CE235A3.cpp). All of the files should be placed in a single folder which should then be zipped for submission to the coursework submission system (FASER), i.e. the submission should be a single file in zip format.
After you complete your programs, you should demonstrate them to the module supervisor.
Late submission and plagiarism
This assignment is to be done individually, i.e. whatever you submit must be your own individual work. Any software or any other materials that you use in this assignment, whether previously published or not, must be referred to and properly acknowledged. Please be aware that the module supervisor may ask students for an interview to explain their submitted work.
Please refer to the Undergraduate Students’ Handbook for details of the School policy regarding late submission and University regulations regarding plagiarism:
http://www.essex.ac.uk/ldev/resources/plagiarism/default.aspx
http://www.essex.ac.uk/about/governance/policies/academic-offences.aspx
因为专业,所以值得信赖。如有需要,请加 QQ:99515681 或 微信:codehelp
今天关于hdu2196 Computer【树形 DP】【换根法】和树根的更换的分享就到这里,希望大家有所收获,若想了解更多关于3D Computer Grapihcs Using OpenGL - 18 相机移动、A Self-Organized Computer Virus Demo in C、An Introduction to Computer Thinking、CE235 Computer Security等相关知识,可以在本站进行查询。
本文标签: