반응형
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    pid_t pid;
    int status;

    switch(pid = fork()){
        case -1 :
            perror("fork");
            exit(1);
            break;
        case 0:
            printf("--> Child Process\n");
            exit(3);
            break;
        default:
            while(wait(&status) != pid)
                continue;
            printf("--> Parent process - My PID:%d\n", (int)getpid());
            printf("Status: %d, %x\n, status, status");
            //자식프로세스의 종료 상태값을 알 수 있다.
            printf("Child process Exit status:%d\n", status >> 8);
            break;
    }

    return 0;
}

 

반응형
반응형
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

int main() {
    pid_t pid;

    switch(pid = fork()){
        case -1 :
            perror("fork");
            exit(1);
            break;
        case 0:
            printf("--> Child Process\n");
            if(execlp("ls", "ls", "-a", (char *)NULL) == -1){
                perror("exelcp");
                exit(1);
            }
            exit(0);
            break;
        default:
            printf("--> Parent process - My PID:%d\n", (int)getpid());
            break;
    }
    
    return 0;
}

 

반응형
반응형
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

int main() {
    pid_t pid;

    switch (pid = fork()){
        case -1 :
            perror("fork");
            exit(1);
            break;
        case 0:
            printf("Childe Process - My PID:%d, My Parent's PID:%d\n", (int)getpid(), (int)getppid());
            break;
        default :
            printf("Parent process - My PID:%d, My Parent's PID:%d, My Child's PID:%d\n", (int)getpid(), (int)getppid(), (int)pid);
            break;
    }

    printf("End of fork\n");
    return 0;
}
반응형
반응형
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

int main() {

    int i;
    time_t t;
    struct tms mytms;
    clock_t t1, t2;

    if((t1 = times(&mytms)) == -1){
        perror("times 1");
        exit(1);
    }

    for(i = 0; i< 99999999; i++)
        time(&t);

    if((t2 = times(&mytms)) == -1){
        perror("times 2");
        exit(1);
    }

    printf("Real time : %.1f sec\n", (double)(t2- t1)/ CLK_TCK);
    printf("User time : %.1f sec\n", (double)mytms.tms_utime / CLK_TCK);
    printf("System time : %.1f sec\n", (double)mytms.tms_stime/ CLK_TCK);


    return 0;
}

 

--output--

Real time : 5.3 sec
User time : 5.2 sec
System time : 0.0 sec
반응형
반응형
#include <stdio.h>
#include <time.h>

int main() {
    struct tm *tm;
    time_t t;

    time(&t);
    printf("Time(sec) : %d\n", (int)t);

    tm = gmtime(&t);
    printf("GMTIME=Y:%d ", tm->tm_year);
    printf("M:%d ", tm->tm_mon);
    printf("D:%d ", tm->tm_mday);
    printf("H:%d ", tm->tm_hour);
    printf("M:%d ", tm->tm_min);
    printf("S:%d\n", tm->tm_sec);

    tm = localtime(&t);
    printf("LOCALTIME=Y:%d ", tm->tm_year);
    printf("M:%d ", tm->tm_mon);
    printf("D:%d ", tm->tm_mday);
    printf("H:%d ", tm->tm_hour);
    printf("M:%d ", tm->tm_min);
    printf("S:%d\n", tm->tm_sec);
    
    return 0;
}

 

--output--

Time(sec) : 1664801617
GMTIME=Y:122 M:9 D:3 H:12 M:53 S:37
LOCALTIME=Y:122 M:9 D:3 H:21 M:53 S:37

Year은 1900년부터 시작이므로 1900 + 122 = 2022년이고,

M은 month로 0부터 읽기 때문에 9면 10월이다

반응형
반응형

https://www.acmicpc.net/problem/2213

 

2213번: 트리의 독립집합

첫째 줄에 트리의 정점의 수 n이 주어진다. n은 10,000이하인 양의 정수이다. 1부터 n사이의 정수가 트리의 정점이라고 가정한다. 둘째 줄에는 n개의 정수 w1, w2, ..., wn이 주어지는데, wi는 정점 i의

www.acmicpc.net

1. 문제설명

서로 인접하지 않은 노드의 집합 중 합이 가장 큰 집합을 찾아야하는 문제이다.

트리를 순회하면서 각 노드마다 선택된 상태와 선택되지 않은 상태를 나누어서 DP로 문제를 해결해야한다.

이 문제에서는 독립집합의 노드를 모두 구해야하는데 이 부분이 어렵다

 

독립집합의 노드를 구하는 방법은

1. 현재 노드를 택한 경우가 택하지 않은 경우보다 결괏값이 커야한다.

2. 다시 트리를 순회하면서 이전 노드와 인접하지 않은 노드를 구해야한다.

 

1, 2번을 모두 만족시키는 노드를 골라야 정답이 된다.

 

 

 


 

 

2. 문제풀이코드 C++

#include <bits/stdc++.h>

using namespace std;
#define MX 10001

int n, weight[MX], dp[MX][2];
vector<int> adj[MX];
bool vis[MX];
vector<int> Path;

void DFS(int node) {
    vis[node] = 1;
    dp[node][1] = weight[node];

    for (auto nx: adj[node]) {
        if (vis[nx]) continue;

        DFS(nx);
        dp[node][1] += dp[nx][0];
        dp[node][0] += max(dp[nx][0], dp[nx][1]);

    }
}

void tracing(int cur, int pre){
    if(dp[cur][1] > dp[cur][0] && !vis[pre]){
        vis[cur] = 1;
        Path.push_back(cur);
    }

    for(auto nx: adj[cur]){
        if(nx==pre) continue;
        tracing(nx, cur);
    }
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> weight[i];
    }

    int a, b, cnt = 0;

    for (int i = 0; i < n - 1; i++) {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    DFS(1);
    memset(vis, 0 , sizeof(vis));
    tracing(1,0);
    sort(Path.begin(), Path.end());


    cout << max(dp[1][0], dp[1][1]) << '\n';
    for(auto num: Path){
        cout << num << ' ';
    }

    return 0;
}
반응형
반응형

https://www.acmicpc.net/problem/2533

 

2533번: 사회망 서비스(SNS)

첫 번째 줄에는 친구 관계 트리의 정점 개수 N이 주어진다. 단, 2 ≤ N ≤ 1,000,000이며, 각 정점은 1부터 N까지 일련번호로 표현된다. 두 번째 줄부터 N-1개의 줄에는 각 줄마다 친구 관계 트리의 에

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;
#define MX 1000001


int N, dp[MX][2];
vector<int> adj[MX];
bool vis[MX];

void DFS(int node){
    vis[node] = 1;

    dp[node][0] = 1;
    for(auto nx : adj[node]){
        if(vis[nx]) continue;
        DFS(nx);

        dp[node][0] += min(dp[nx][0], dp[nx][1]);
        dp[node][1] += dp[nx][0];
    }
}


int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> N;
    for(int i=0; i<N-1; i++){
        int u,v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    DFS(1);

    cout << min(dp[1][0], dp[1][1]) << '\n';


    return 0;
}
반응형
반응형

https://www.acmicpc.net/problem/15681

 

15681번: 트리와 쿼리

트리의 정점의 수 N과 루트의 번호 R, 쿼리의 수 Q가 주어진다. (2 ≤ N ≤ 105, 1 ≤ R ≤ N, 1 ≤ Q ≤ 105) 이어 N-1줄에 걸쳐, U V의 형태로 트리에 속한 간선의 정보가 주어진다. (1 ≤ U, V ≤ N, U ≠ V)

www.acmicpc.net

 

#include <bits/stdc++.h>
using namespace std;
#define MX 100001

int N, R, Q, dp[MX];
vector<int> adj[MX];
bool vis[MX];

int DFS(int node){
    dp[node] = 1;

    for(int i=0; i<adj[node].size(); i++){
        if(dp[adj[node][i]]==0)
            dp[node] += DFS(adj[node][i]);
    }

    return dp[node];
}


int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> N >> R >> Q;
    for(int i=0; i<N-1; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    DFS(R);

    for(int i=0; i<Q; i++){
        int num;
        cin >> num;
        cout << dp[num] << '\n';
    }

    return 0;
}

 

반응형

+ Recent posts